vendredi 30 août 2019

sample() keeps returning the same number

I watch movies from a bucket list with my friend containing 100 movies. I have this small script that is supposed to randomly select our next movie each time I run it.

Films <- 1:100
Gekeken <- c(2,6,30,45,86,14,22,40,13,15,16)
sample(Films[-Gekeken],1)

I append the last number to the vector Gekeken manually after running it and run the script again prior to next movie night. There is no seed set, so I would expect sample to pick a new number randomly from the vector Films[-Gekeken] each time I run the script.

However, since I've started using this script (after movie no 40) it seems to pick the 11th value in the vector each time (i.e. 13, 15, 16 and for the current script 17 comes up each time I run the script in a fresh R session).

My question, why does this script behave this way? How do I make sure the first selected number is (quasi)random in a fresh R session? I feel like I'm missing something here but can't find any information about this issue.

My session info:

R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C                      
[5] LC_TIME=Dutch_Netherlands.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.4 tools_3.4.4    yaml_2.1.18   

I've tried running the same script in R version 3.6.1, but nothing changes. I know it would be easy to get a random order of all 100 numbers in one go, but we like the surprise.




Aucun commentaire:

Enregistrer un commentaire