In the AsemblyScript book it mentions that Math.random() takes a seed and returns an <f64> value. I just need a random <u64> value. How do I achive that?
I tried
(Math.random() * 0xffffffffffffffff) as u64
<u64>(<f64>Math.random() * <f64>0xffffffffffffffff)
(<f64>Math.random() * <f64>0xffffffffffffffff) as u64
or with f64.MAX_VALUE in the place of 0xffffffffffffffff and whatnot.
but I keep getting 0.
I can get <U32> random values just fine but When I multiply two <U32> random values I get like 52 random bits and the rest is 0. I understand why this happens from my JS background, still from the typed struct and lower level abstractions of AS I hoped to get no friction.
How exactly I can obtain a <u64> random integer properly with AssemblyScript?
Edit:
I think I finally got it doing like
(<u64>(Math.random() * u32.MAX_VALUE) << 32) | <u32>(Math.random() * u32.MAX_VALUE)
but is this really how it should be done?
After reading your question, I decided to look into how AssemblyScript implements
Math.randomin the standard library, to see if I can get some inspiration to solve your problem.Funny, enough, it seems to use the
murmurhash3and some custom extra hashes. Right before the return, it reinterprets au64value into af64, after some extra processing:I got curious if we can use that
u64value directly as a number in a random sequence, so I reworked the bits involved and posted it on github here, but the main function is:A very brief test shows that, at least at a glance, it produces fairly good random results: