# Re: Pseudo Random Number Generation across PicoLisp implementations

```H Christophe,

> final static long initSeed(Any x) {
>
> Means it gives back a signed 64 bits integer for anything we threw at
> it (called x).```
```
Yes.

> long n; for (n = 0; x instanceof Cell; x = x.Cdr) n += initSeed(x.Car);
>
> Is a recursive trick that walks through x (if walkable, ie Cell) and cumulates
> in n the results of initSeed for anything non walkable that was thrown at it.
>
>
> if (x != Nil) {…
>
> When the non walkable is not a list termination…

Right.

> if (x instanceof Number && ((Number)x).Big == null) n += ((Number)x).Cnt;

Yes. For efficiency, a Number may internally hold a short number (32
bits) in 'Cnt', or a big number in 'Big'.

> If x is a 32 bits integer, just add it to n (not sure about the (Number) cast,
> Number doesn't seem to have children).

The cast is only needed to keep the compiler happy. As 'x' is of type
'Any', it would complain that there is no 'Cnt' property.

In any case, as you observed, we accumulated all that stuff into a long
number 'n'.

> return n>=0? n*2 : -n*2+1;
>
> This ensures the return value is non negative (?).
> But why would not the result overflow?

Yes, it may overflow. The above operation is to keep the sign bit in bit
zero of the result. The sign bit is more important than the highest bit
of the number.

> Now if I'm correct until here, initSeed(1) should just return 2.

Correct.

> Then I don't understand why (seed 1) returns -5718471626015965606,
> since (seed x) just multiplies the result of initSeed(x) by
> 6364136223846793005,
> and 2*6364136223846793005<2^64 (no overflow).

It does overflow. Though 2*6364136223846793005 = 12728272447693586010
fits into 64 bits, it has its most significant bit set. As Java uses
2-complement representation, this is a negative number.

A negative number in 2-complement representation has all bits inverted
plus 1

:  (inc (x| (dec (** 2 64)) (* 2 6364136223846793005)))
-> 5718471626015965606

♪♫ Alex
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
```