Econometrics with R

set.seed(1)

# rolling a dice three times in a row
sample(1:6, 3, replace = T)

># 1 4 1

set.seed(2)

# rolling a dice three times in a row
sample(1:6, 3, replace = T)

5 6 6

Hi R community,

I just began practising R with Econometrics and I'm starting with the very basics of probability where I have recently been confused about the function set.seed()
According to my understanding of this function, it works for the reproducibility of random number generation. However, my question is, does set.seed(1) means that the first number among a set of randomly generated numbers will be 1? Because as seen from the results posted above, the first number of my RNG sample was not 2 even though I used set.seed(2).


set.seed(1)

# rolling a dice three times in a row
sample(1:6, 3, replace = T)
dice <- sample(1:6, 3, replace = T)
mean(dice)

3.3333

Next question, why is the result of the mean (dice) above 3.3333 whereas the result of mean(1:6, 3, replace = T) is 3.5. I thought the function dice <- sample(1:6, 3, replace = T) was supposed to assign value to dice. Why are the results generated different?

Hi. The set.seed function initializes the random number generation process, but its argument within the parentheses does not guarantee it will equal the first random number. If you had used set.seed(999), sample(1:6,3), you would not expect the first random number to be 999.

The mean of 1:6 is 3.5. But the result of sample(1:6,3) may be any three integers. If you're curious, print the results of dice, and then calculate mean(dice).

1 Like

oh I just printed the result of dice and it is 2 5 3. No wonder. Wow. Thank you! And yes, you are right about the logic with set.seed :grinning:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.