Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Number Generator for R 3.6.1

Tags:

r

I have the latest version of R (3.6.1), but when I use functions that are using the random number generator, they default to an older (i.e. pre-3.6.0) RNG which uses Rounding instead of Rejection for sampling. I am not sure why this is happening, and would appreciate your help resolving it.

set.seed(1)
sample(20)
RNGkind()
R.version

Below are the results of my run:

set.seed(1)
sample(20)
# 6  8 11 16  4 14 15  9 19  1  3  2 20 10  5  7 12 17 18 13

RNGkind()
# "Mersenne-Twister" "Inversion"        "Rounding"

R.version

platform       x86_64-w64-mingw32                         
arch           x86_64                                     
os             mingw32                                    
system         x86_64, mingw32                            
status         Patched                                    
major          3                                          
minor          6.1                                        
year           2019                                       
month          09                                         
day            06                                         
svn rev        77160                                      
language       R                                          
version.string R version 3.6.1 Patched (2019-09-06 r77160)
nickname       Action of the Toes        

Based on the NEWS and the linked discussion, I am expecting the output of RNGkind() to look as follows instead:

# "Mersenne-Twister" "Inversion"        "Rejection"

Am I misunderstanding the NEWS?

like image 988
Dave Avatar asked Sep 10 '19 16:09

Dave


People also ask

Does R have a random number generator?

Random Number Generator in R is the mechanism which allows the user to generate random numbers for various applications such as representation of an event taking various values, or samples with random numbers, facilitated by functions such as runif() and set.

How do you generate a random whole number in R?

For uniformly distributed (flat) random numbers, use runif() . By default, its range is from 0 to 1. To generate numbers from a normal distribution, use rnorm() . By default the mean is 0 and the standard deviation is 1.

What is random seed in R?

. Random. seed is an integer vector, containing the random number generator (RNG) state for random number generation in R. It can be saved and restored, but should not be altered by the user. RNGkind is a more friendly interface to query or set the kind of RNG in use.

How do you generate a random number in range in Python?

Use a random. randrange() function to get a random integer number from the given exclusive range by specifying the increment. For example, random. randrange(0, 10, 2) will return any random number between 0 and 20 (like 0, 2, 4, 6, 8).


2 Answers

As suggested by @JanvanderLaan in the comments, a possible problem might stem from an .RData file being loaded upon start up. For example if one had a previous version of R installed an every used it, the initial working directory from getwd() upon starting up a session will contain an .RData file and a .Rhistory file, if one ever saved the session. Usually this is the documents folder on windows if one uses Rstudio, which most individuals goes out of their way to clear of old or unusual files.

Following the suggestion in the comment, going to the directory output by getwd() in a fresh R session, I found an .RData file, closed the existing R sessions without saving the current session, and reopened a new R session. And it seems to have correctly fixed the problem as can be seen below. Thus it seems the method for generating random numbers is indeed saved between sessions within the .Rdata file.

RNGkind()
[1] "Mersenne-Twister" "Inversion"        "Rejection"   

Edit (illustration)

We can actually quite easily illustrate this in a fresh R session, regardless of which random number generator is set. Assuming one has ever opened and saved an R session prior to R-3.6.1, the following code illlustrates the problem

#Assuming that the R session has just opened
>RNGkind()
[1] "Mersenne-Twister" "Inversion"        "Rounding"  
>RNGversion("3.6.1") 
>RNGkind()
[1] "Mersenne-Twister" "Inversion"        "Rejection"  
>load(".RData", verbose = TRUE)
Loading objects:
  .Random.seed
>RNGkind()
[1] "Mersenne-Twister" "Inversion"        "Rounding"  

As can be seen, it stores the .Random.seed, however what is not shown is that the type of random number generator is also imported, upon loading the previous environment. Executing

file.remove(".RData")
q("no")

should thus fix the issue for future sessions, assuming working directory has not been changed in the current session.

like image 94
Oliver Avatar answered Sep 22 '22 07:09

Oliver


I had hoped that setting a bounty would extract a definite answer to what caused OP's problem. While that didn't happen, some comments and answers suggested a few reasons. I provide an additional answer here to put them all in one place and provide a little better illustration for how to tell when one thing is happening versus the other.

Suggested causes:

  • Seed being set by a .RData file in the initial working directory
  • RNG type being set by .Rprofile
  • Bug in a recent patch

Seed set by a .RData file

As discussed in Oliver's answer, this could be caused by a .RData file in your initial working directory. I won't go into much more detail (you can consult the linked answer for that), but I did want to show what you would see on startup if that were the case. This is what the start up message in R looks like on my machine:

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>

If you are reading in a .RData file on startup that could cause that, you'd see a notification about that right after the last paragraph of the startup message:

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Workspace loaded from ~/.RData]

RNG type set by .Rprofile

.Rprofile is a script that runs on startup that you can use to set some settings at the outset of your session. (You can read a little more about it here or here, or in the R documentation). Though I doubt this is the case for you, it is at least possible the problem was caused by a .Rprofile file being run with a line something like the following

RNGkind(sample.kind = "Rounding")

If you had such a setting in a .Rprofile file that was causing your problem, you'd see a warning at the end of your startup message:

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

Warning message:
In RNGkind(sample.kind = "Rounding") : non-uniform 'Rounding' sampler used

Bug

If you see neither of those messages at startup, my best guess is that this is caused by some kind of bug introduced in a recent patch to R 3.6.1. I kind of hesitate to say that, but I can't see another option (I had kind of hoped that offering a bounty would draw an answer that provided such another option). If so, I'd report it as a bug; find out more here.

like image 27
duckmayr Avatar answered Sep 25 '22 07:09

duckmayr