jeudi 30 juillet 2020

Different results from sample with and without prob parameter

I noticed sample returns different results when the parameter prob is used to indicate uniform distribution and when prob is omitted, despite the fact that the function will still generate numbers from a uniform distribution.

This has been noticed before (see R sample probabilities: Default is equal weight; why does specifying equal weights cause different values to be returned?) and answers pointed out how that the c routines for sample are different when prob is NULL and when it's not.

Is there a reason why this is happening?
Wouldn't it be preferable to return the same results every time the distribution to generate data is the same?

Example:

set.seed(1)
sample(c(0,1), 10, replace = T, prob = c(0.5, 0.5))

 [1] 1 1 0 0 1 0 0 0 0 1


set.seed(1)
sample(c(0,1), 10, replace = T)

 [1] 0 1 0 0 1 0 0 0 1 1




Aucun commentaire:

Enregistrer un commentaire