Tunarock
Tunarock

Reputation: 127

Avoiding pseudorandom number generation in R

How can I randomly sample n integers (let's say from 1 to 200) avoiding the pseudorandom problem? I'm currently using sample(), but the sequence generated is the same every time I run my code.

Upvotes: 1

Views: 109

Answers (1)

nrussell
nrussell

Reputation: 18602

Having not seen your code, it sounds like you are (unintentionally) reseeding the RNG. At any rate, if you are interested in avoiding any possible issues that stem from PRNGs you can use the random package, which utilizes random.org's true random number generator. For example,

set.seed(123)
R> random::randomNumbers(10, 1, 200, col = 1)
#       V1
# [1,] 178
# [2,] 171
# [3,]  20
# [4,]  47
# [5,] 113
# [6,]  75
# [7,] 120
# [8,] 125
# [9,]  98
# [10,]  15

set.seed(123)
R> random::randomNumbers(10, 1, 200, col = 1)
#       V1
# [1,] 112
# [2,]  84
# [3,]  83
# [4,]  20
# [5,]  19
# [6,] 112
# [7,]  64
# [8,] 134
# [9,] 105
# [10,]  63

Upvotes: 2

Related Questions