Skip to content
Prev 45038 / 398528 Next

LCG with modulo 2^30

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?

Sean
Enayetur Raheem wrote: