I'm trying to port the old C standard rand() function over to Javascript just for testing purposes. I don't plan to use this in a practical scenario so please don't freak out about it being insecure. This is the C Implementation of the function
seed = seed * 1103515245 + 12345;
return (seed/65536) % 32768;
Where 32768 was RAND_MAX so I tried porting that over to Javascript
Random = function(p){
this.s = p;
this.rand = function(){
this.s = this.s*1103515245 + 12345;
return Math.floor((this.s/65536) % 32768);
}
}
let r = new Random(new Date()/1000);
When I call r.rand() the first time, it produces the same result on the C side when I seed it with time(null) which is seconds since epoch. But then every successive call to r.rand() just gives me 0 and I'm curious as to why..
r.rand(); //gives expected results
r.rand(); //second call produces 0
Aucun commentaire:
Enregistrer un commentaire