Martin Møller Skarbiniks Pedersen
2017-Aug-14 16:49 UTC
[R] Help creating the IBM Randu function
Dear all, I am trying to learn functions in R and 3D plotting so I decided to try to plot the famous bad PRNG Randu from IBM(1). However something is not correct in the function I have created. First I define the function RANDU like this:> RANDU <- function(num) { return (65539*num)%%(2^31) }and test that it works for a seed of 1:> RANDU(1)[1] 65539 but if I want the next value in the sequence I get this number.> (65539*65539)%%(2^31)[1] 393225 However using the RANDU function twice doesn't give the same result as above.> RANDU(RANDU(1))[1] 4295360521 I expect these two values to be the same but that is not the case. 393225 should be the correct. I guess it might be something with local vs. global environment ?! Please advise and thanks. Regards Martin M. S. Pedersen (1) https://en.wikipedia.org/wiki/RANDU [[alternative HTML version deleted]]
Hi Martin, The corrected function would be RANDU <- function(num) { return ((65539*num)%%(2^31)) } You forgot the brackets for the return function. Hence, what was returned was always (65539 * num) On Mon, Aug 14, 2017 at 12:49 PM, Martin M?ller Skarbiniks Pedersen <traxplayer at gmail.com> wrote:> Dear all, > > I am trying to learn functions in R and 3D plotting so I decided to try > to plot > the famous bad PRNG Randu from IBM(1). > However something is not correct in the function I have created. > First I define the function RANDU like this: > >> RANDU <- function(num) { return (65539*num)%%(2^31) } > > and test that it works for a seed of 1: >> RANDU(1) > [1] 65539 > > but if I want the next value in the sequence I get this number. >> (65539*65539)%%(2^31) > [1] 393225 > > However using the RANDU function twice doesn't give the same result as > above. > >> RANDU(RANDU(1)) > [1] 4295360521 > > I expect these two values to be the same but that is not the case. > 393225 should be the correct. > > I guess it might be something with local vs. global environment ?! > > Please advise and thanks. > > Regards > Martin M. S. Pedersen > > (1) https://en.wikipedia.org/wiki/RANDU > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Huzefa Khalil PhD Candidate, Department of Political Science, University of Michigan
Please look at ?datasets::randu for David Donoho's translation of RANDU into R. On Mon, Aug 14, 2017 at 12:49 PM, Martin M?ller Skarbiniks Pedersen <traxplayer at gmail.com> wrote:> Dear all, > > I am trying to learn functions in R and 3D plotting so I decided to try > to plot > the famous bad PRNG Randu from IBM(1). > However something is not correct in the function I have created. > First I define the function RANDU like this: > >> RANDU <- function(num) { return (65539*num)%%(2^31) } > > and test that it works for a seed of 1: >> RANDU(1) > [1] 65539 > > but if I want the next value in the sequence I get this number. >> (65539*65539)%%(2^31) > [1] 393225 > > However using the RANDU function twice doesn't give the same result as > above. > >> RANDU(RANDU(1)) > [1] 4295360521 > > I expect these two values to be the same but that is not the case. > 393225 should be the correct. > > I guess it might be something with local vs. global environment ?! > > Please advise and thanks. > > Regards > Martin M. S. Pedersen > > (1) https://en.wikipedia.org/wiki/RANDU > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On 14/08/2017 2:18 PM, Huzefa Khalil wrote:> Hi Martin, > > The corrected function would be > > RANDU <- function(num) { return ((65539*num)%%(2^31)) } > > You forgot the brackets for the return function. > Hence, what was returned was always (65539 * num)Yes, this is one disadvantage of having a return function rather than reserved word. It is also an argument for avoiding the use of return(); Martin's function could be written as RANDU <- function(num) { (65539*num)%%(2^31) } or even RANDU <- function(num) (65539*num)%%(2^31) since the braces aren't needed when a function body has only a single statement. Duncan Murdoch> > > On Mon, Aug 14, 2017 at 12:49 PM, Martin M?ller Skarbiniks Pedersen > <traxplayer at gmail.com> wrote: >> Dear all, >> >> I am trying to learn functions in R and 3D plotting so I decided to try >> to plot >> the famous bad PRNG Randu from IBM(1). >> However something is not correct in the function I have created. >> First I define the function RANDU like this: >> >>> RANDU <- function(num) { return (65539*num)%%(2^31) } >> >> and test that it works for a seed of 1: >>> RANDU(1) >> [1] 65539 >> >> but if I want the next value in the sequence I get this number. >>> (65539*65539)%%(2^31) >> [1] 393225 >> >> However using the RANDU function twice doesn't give the same result as >> above. >> >>> RANDU(RANDU(1)) >> [1] 4295360521 >> >> I expect these two values to be the same but that is not the case. >> 393225 should be the correct. >> >> I guess it might be something with local vs. global environment ?! >> >> Please advise and thanks. >> >> Regards >> Martin M. S. Pedersen >> >> (1) https://en.wikipedia.org/wiki/RANDU >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > >
>>>>> Richard M Heiberger <rmh at temple.edu> >>>>> on Mon, 14 Aug 2017 14:36:40 -0400 writes:> Please look at ?datasets::randu > for David Donoho's translation of RANDU into R. Thanks a lot, Rich, for pointing to this: Indeed, the RANDU aka 'randu' data set has been part of R since the last millennium (and hence before R reached version 1.0.0). The help page does mention where we got the data set originally, namely Dave Donoho. However, I would bet he did not use R at the time, and indeed it was Prof Brian Ripley who added the R code to R sources in 1999 ------------------------------------------------------------------------ r5632 | ripley | 1999-08-27 08:59:05 +0200 (Fri, 27. Aug 1999) improve clarity (I hope), add an R example to re-generate this ------------------------------------------------------------------------ and yes, indeed example(randu) already creates the RANDU data set for you .. since 1999. One curious thing: Martin .. Pedersen's Wikipedia page (1) seems to make it clear it stems from IBM whereas in R's help page help(randu) we only mention that we got it through VAX VMS code. > On Mon, Aug 14, 2017 at 12:49 PM, Martin M?ller Skarbiniks Pedersen > <traxplayer at gmail.com> wrote: >> Dear all, >> >> I am trying to learn functions in R and 3D plotting so I decided to try >> to plot >> the famous bad PRNG Randu from IBM(1). >> However something is not correct in the function I have created. >> First I define the function RANDU like this: >> >>> RANDU <- function(num) { return (65539*num)%%(2^31) } >> >> and test that it works for a seed of 1: >>> RANDU(1) >> [1] 65539 >> >> but if I want the next value in the sequence I get this number. >>> (65539*65539)%%(2^31) >> [1] 393225 >> >> However using the RANDU function twice doesn't give the same result as >> above. >> >>> RANDU(RANDU(1)) >> [1] 4295360521 >> >> I expect these two values to be the same but that is not the case. >> 393225 should be the correct. >> >> I guess it might be something with local vs. global environment ?! >> >> Please advise and thanks. >> >> Regards >> Martin M. S. Pedersen >> >> (1) https://en.wikipedia.org/wiki/RANDU >> >> [[alternative HTML version deleted]]