# [R] LCG with modulo 2^30

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Feb 29 08:48:11 CET 2004

```One quick addition:

1:2^31 fails because it is trying to create a sequence of integers, and
the largest integer in R is 2^31-1.  For the same sort of reason, the
largest vector you can create is 2^31-1 elements, but on a 32-bit machine
you will run out of address space at 2^29 doubles, and very likely out of
memory before that (as on most 32-bit OSes allow a user process to access
2 or 3Gb rather than the theoretical maximum of 4Gb, to allow for a
stack, for example).

On Sun, 29 Feb 2004, Sean O'Riordain wrote:

> 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:
>
> >
> > 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
> > http://www.R-project.org/posting-guide.html
> >
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```