jscl icon indicating copy to clipboard operation
jscl copied to clipboard

Random not implemented

Open morganastra opened this issue 11 years ago • 7 comments

The RANDOM function, MAKE-RANDOM-STATE, etc are not implemented.

I think RANDOM could probably just be a thin wrapper around javascript's Math.random()?

morganastra avatar Apr 29 '13 11:04 morganastra

Unfortunately default Javascript random generator cannot be seeded, so to support random state functions a custom pseudorandom generator will have to be used.

On Mon, Apr 29, 2013 at 1:48 PM, RadioTelescope [email protected]:

The RANDOM function, MAKE-RANDOM-STATE, etc are not implemented.

I think RANDOM could probably just be a thin wrapper around javascript's Math.random()?

— Reply to this email directly or view it on GitHubhttps://github.com/davazp/jscl/issues/52 .

6502 avatar Apr 29 '13 13:04 6502

Oh, that's unfortunate.

So, here's a potentially crazy idea: most programs that use random numbers just (setf *random-state* (make-random-state t)) at the top and then call random wherever it's needed, right? What if we made (make-random-state t) return a magic value that tells the CL random to use JS Math.random(). We could still implement a mersenne twister PRNG that works the normal way when *random-state* is set to something other than the magic value.

morganastra avatar Apr 29 '13 15:04 morganastra

No you cannot in general. Random state can be copied so if you start using Javascript random you must also start storing all random numbers extracted after the first copy of the state is made. Not sure if this is worth the effort... probably just implementing or getting somewhere a decent random is better. By the way in the past I've been facing truly horrible random generators in javascript so there's nothing much worth to save. My nexus 1 random generator is so bad that's unusable even for random shuffling songs... it was the same in Chrome, but they fixed Chrome and left android broken; see http://stackoverflow.com/questions/5531827/random-point-on-a-given-sphere

6502 avatar Apr 29 '13 19:04 6502

Okay fair enough. I'll just implement a simple version of the algorithm sbcl uses then. On Apr 29, 2013 3:51 PM, "Andrea Griffini" [email protected] wrote:

No you cannot in general. Random state can be copied so if you start using Javascript random you must also start storing all random number extracted at the first copy is made. Not sure if this is worth the effort... probably just implementing or just getting somewhere a decent random is better. By the way in the past I've been facing truly horrible random generators in javascript so there's nothing much worth to save. My nexus 1 random generator is so bad that's unusable even for random shuffling songs... it was the same in Chrome, but they fixed Chrome and left android broken; see http://stackoverflow.com/questions/5531827/random-point-on-a-given-sphere

— Reply to this email directly or view it on GitHubhttps://github.com/davazp/jscl/issues/52#issuecomment-17189841 .

morganastra avatar Apr 29 '13 22:04 morganastra

It is very unfortunate, but there are a few things like this. For example, sleep will not be possible either I guess.

davazp avatar May 01 '13 14:05 davazp

Would a member of the PCG family be sufficient for this, or do we demand cryptographic security from the generator also?

equwal avatar Jul 19 '18 21:07 equwal

@equwal I'm not familiar with random number generators but, I would say that it should be sufficient. If you want something secure you can FFI to some JS library. (We can do that for the standard functions as well).

davazp avatar Jul 20 '18 06:07 davazp