[R] LCG with modulo 2^30

Sean O'Riordain seanpor at acm.org
Sun Feb 29 07:17:23 CET 2004

Hi Raheem,

Firstly - fair warning...I'm not an R expert at all!  However it is my 
understanding that the expression "for i in 1:m" creates a full vector 
in memory of all the consecutive numbers 1 to m... (i presume these are 
4-byte ints here otherwise it would have fallen over before 2^29), but 
taking the minimal assumption of ints these take 4 bytes... 1:2^29 
requires (2^29)*4 bytes of memory - running on a 32-bit platform you 
have an absolute maximum of about 2^32 bytes of addressable memory (some 
memory will be taken by the os...).

if you start R and say "gc()" it'll tell you about memory allocated... 
then try k<-c(1:2^24); gc() and you might get a response something like...
 > gc()
         used (Mb) gc trigger (Mb)
Ncells 208431  5.6     407500 10.9
Vcells  73157  0.6     786432  6.0
 > k <- c(1:2^24)
 > gc()
          used (Mb) gc trigger  (Mb)
Ncells  208431  5.6     407500  10.9
Vcells 8428997 64.4   17108043 130.6

if you REALLY want to interate to 2^32 without allocating a huge memory 
vector like that try using your own counter and a while loop instead 
which won't allocate more memory than you have - but this will likely be 
SLOW... something along the lines of
i <- 0
while (i < 2^32) {
    # now we do stuff
    i <- i+1

i'm quite sure that there are more 'R-ish' ways of doing things...

btw. what are you trying to achieve?


Enayetur Raheem wrote:

> I can't run a function which generates random numbrers using linear 
> congruential generator. My multiplier is a=5+8^6, increment is b=1 and 
> modulo is m=2^30.
> the code I have written works for modulo upto  m=2^28.
> For m= 2^29 , it says, can not allocate memory for the vector or 
> something like that.
> For m= 2^31 or more, its says the argument  "for i in 1:m "  is too 
> large in magnitude.
> I tried to increase the memory size but it did not  work.
> Any help will be appreciated.
> Thanks.
> Raheem
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html

More information about the R-help mailing list