smol-rs / fastrand

A simple and fast random number generator
Apache License 2.0
386 stars 33 forks source link

Remove unnecessary seed modifcation #73

Closed ironhaven closed 4 months ago

ironhaven commented 11 months ago

Because the first step of wyrand is an addition with a constant, a non-zero seed is not required

notgull commented 6 months ago

This slipped through the cracks. I'm not familiar enough with wyrand to know if this improves or doesn't improve randomness.

Bluefinger commented 6 months ago

This slipped through the cracks. I'm not familiar enough with wyrand to know if this improves or doesn't improve randomness.

This is a holdover from when fastrand was based on PCG, not WyRand. And the C reference implementation doesn't do anything to avoid the non-zero seed: https://github.com/wangyi-fudan/wyhash/blob/master/wyhash.h#L151

However, if we modify the seed, it will a breaking change as the output will no longer quite be the same per sequence. And probably, if we are to change the output, I would suggest updating the constants in fastrand to use the final v4.2 ones as defined in the C reference, which should help to improve the entropy quality somewhat (though that can be another PR).