# Re: Pseudo Random Number Generation across PicoLisp implementations

```Hi,
After some more time to investigate, I found out that the LCG used in
PicoLisp was not the Haynes one, but the one referred as «Newlib» here:
http://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
More auto-answers and other questions below.```
```
On Wed, Apr 22, 2015 at 10:50 PM, Christophe Gragnic
<christophegrag...@gmail.com> wrote:
> Now the question. I found discrepancies between PicoLisp:
> : (rand 1 1000)
> -> 1
> : (rand 1 1000)
> -> 934
> : (bye)
>
> And Ersatz
> : (rand 1 1000)
> -> 1
> : (rand 1 1000)
> -> 967
> : (bye)
>
> I tried to inspect the source for getting some explanation with no luck.
> PicoLisp
> Ersatz:
> Is there a reason for those different behaviours ?

Maybe I was too tired. The difference is 33, and we see in the Ersatz src:
n = (int)(Seed >>> 33) % n;
But it is a coincidence since a bit shift should not have this effect
(confirmed when trying with the args 0 and 1000):
PicoLisp:
: (rand 0 1000)
-> 0
: (rand 0 1000)
-> 648
: (rand 0 1000)
-> 760
Ersatz:
: (rand 0 1000)
-> 0
: (rand 0 1000)
-> 824
: (rand 0 1000)
-> 880

Then I had to find the shift in the C code, which is hidden in the `hi` macro
(for high bits):
#define hi(w)           num((w)>>BITS)
Where:
#define WORD ((int)sizeof(long))
#define BITS (8*WORD)

What I don't understand is why Ersatz shifts by 33, and PicoLisp by BITS,
(which should IMHO be 32). This connects to my next remarks/questions:
they start at bit 0, thus should the shift be 32 ?
* Is 33 used because of the sign bit ?
* In the PicoLisp src, why use LL and not ULL to define
6364136223846793005?

chri

--

http://profgra.org/lycee/ (site pro)
http://delicious.com/profgraorg (liens, favoris)