prabhu prabhu - 3 months ago 17
R Question

Random number generation - In equal distribution of variables

I need to generate random numbers with equal number of variables, For example

Variables[A,B,C] (A = 1, B = 2, C = 3)
Between [1,12]


Trial 1:
1 3 2 1 3 3 1 3 2 1 2 3

Trial 2:
3 3 1 1 2 3 1 2 1 1 2 2

In both trials variables were not equally distributed. For each trail I need 4 A's, 4 B's, 4 C's in equal distribution but in random.

Answer

The sample function is used to generate random numbers.

You can specify exactly what values you want to feature in your random ordering by leaving the replace argument as FALSE.

First create all the values you want to feature, repeated as many times as you want them.

vals <- rep(c("A", "B", "C"), each = 4L)

Then sample your selection without replacing:

rsample <- sample(vals, 12L, replace = FALSE)