Bill Allombert on Mon, 11 Jun 2007 19:01:31 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: random sequence reversal


On Mon, Jun 11, 2007 at 04:04:59PM +0200, Alain SMEJKAL wrote:
> Hello all,
> 
> Is there a way to reverse a number sequence generated by the random function
> ?
> More precisely my problem is to retrieve the first generated numbers when
> many random(k) iterations were done with the same k and the only last random
> numbers are known (and maybe useful, the used Pari session is still alive).

You can do something like:
back(k)=local(a,n);a=random();setrand(1);n=0;while(random()!=a,n++);setrand(1);for(i=1,n-k,random())

back(n) will set the generator n step backward.

Note that the generator initial seed is the same in all PARI sessions for
a given version.

> ps : Pari version is 2.3.2

In that case you can do a true reversion:

rev(rand)=542687373*(rand-12347)%2^31

if rand is the last random() value, this return the previous one,
that you can feed to setrand().
? random()
%1 = 1000288896
? random()
%2 = 768462011
? random()
%3 = 892785826
? random()
%4 = 739165157
? rev(%)
%5 = 892785826
? rev(%)
%6 = 768462011
? rev(%)
%7 = 1000288896
? setrand(%)
%8 = 1000288896
? random()
%9 = 768462011
? random()
%10 = 892785826
? random()
%11 = 739165157
? random()
%12 = 1874708212

Note: random(N) actually compute several value of random() and combine
them, and the number of random() step used by a random(N) call depend
on the PARI version.

Cheers,
Bill.