TI-84 Plus Random Number Generator Algorithm -


edit: main question want replicate ti-84 plus rng algorithm on computer, can write in language javascript or lua, test faster.

i tried using emulator, turned out slower calculator.

just people concerned: there question this, answer question says how transfer already-generated numbers on computer. don't want this. tried it, had leave calculator running weekend, , still wasn't done.

the algorithm being used paper efficient , portable combined random number generators p. l'ecuyer.

you can find paper here , download free here.

the algorithm used ti calculators on rhs side of p. 747. i've included picture.

l'ecuyer's algorithm

i've translated c++ program

#include <iostream> #include <iomanip> using namespace std;  long s1,s2;  double uniform(){   long z,k;   k  = s1 / 53668;   s1 = 40014*(s1-k*53668)-k*12211;   if(s1<0)     s1 = s1+2147483563;    k  = s2/52774;   s2 = 40692*(s2-k*52774)-k*3791;   if(s2<0)     s2 = s2+2147483399;    z=s1-s2;   if(z<1)     z = z+2147483562;    return z*(4.656613e-10); }  int main(){   s1 = 12345; //gotta love these seed values!   s2 = 67890;   for(int i=0;i<10;i++)     cout<<std::setprecision(10)<<uniform()<<endl; } 

note initial seeds s1 = 12345 , s2 = 67890.

and got output ti-83 (sorry, couldn't find ti-84 rom) emulator:

ti-83 screenshot

this matches implementation produces

my computer

i've cranked output precision on implementation , following results:

0.9435973904 0.9083188494 0.1466878273 0.5147019439 0.4058096366 0.7338123019 0.04399198693 0.3393625207 

note diverge ti's results in less significant digits. may difference in way 2 processors (ti's z80 versus x86) perform floating point calculations. if so, hard overcome issue. nonetheless, random numbers still generate in same sequence (with caveat below) since sequence relies on integer mathematics, exact.

i've used long type store intermediate values. there's risk ti implementation relies on integer overflow (i didn't read l'ecuyer's paper carefully), in case have adjust int32_t or similar type emulate behaviour. assuming, again, processors perform similarly.

edit

this site provides ti-basic implementation of code follows:

:2147483563→mod1 :2147483399→mod2 :40014→mult1 :40692→mult2  #the randseed algorithm :abs(int(n))→n :if n=0 : 12345→seed1 : 67890→seed2 :else : mod(mult1*n,mod1)→seed1 : mod(n,mod2)→seed2 :endif  #the rand() algorithm :local result :mod(seed1*mult1,mod1)→seed1 :mod(seed2*mult2,mod2)→seed2 :(seed1-seed2)/mod1→result :if result<0 : result+1→result :return result 

i translated c++ testing:

#include <iostream> #include <iomanip> using namespace std;  long mod1  = 2147483563; long mod2  = 2147483399; long mult1 = 40014; long mult2 = 40692; long seed1,seed2;  void seed(int n){   if(n<0) //perform abs     n = -n;   if(n==0){     seed1 = 12345; //gotta love these seed values!     seed2 = 67890;   } else {     seed1 = (mult1*n)%mod1;     seed2 = n%mod2;   } }  double generate(){   double result;   seed1  = (seed1*mult1)%mod1;   seed2  = (seed2*mult2)%mod2;   result = (double)(seed1-seed2)/(double)mod1;   if(result<0)     result = result+1;   return result; }  int main(){   seed(0);   for(int i=0;i<10;i++)     cout<<setprecision(10)<<generate()<<endl; } 

this gave following results:

0.9435974025 0.908318861 0.1466878292 0.5147019502 0.405809642 0.7338123114 0.04399198747 0.3393625248 0.9954663411 0.2003402617 

which match achieved implementation based on original paper.


Comments