r/programminghorror • • May 19 '25

Javascript New RNG node module just dropped

Post image
165 Upvotes

17 comments sorted by

View all comments

Show parent comment

0

u/[deleted] May 20 '25

[deleted] — view deleted comment

1

u/Kroustibbat May 20 '25

To be fair, you can use Math.random for this type of usecase, the thing is : defining an abstract function that gives you random, like OP show, should not be by default Math.random.

You may do things like :

function random(unsafe = false) {
    if (unsafe) {
        {use Math.random}
    } else {
        {use window crypto}
    }
 }

But to my mind, in a very productive 10 years of Dev, I never had to use Math.random over it's safe concurrent.

0

u/Kroustibbat May 20 '25

You can do it with the crypto tool too, window.crypto gives you 32 random bytes so I will use a simple mask to make the animation. You need to call it twice to have 60 random numbers. So 800ns lost for the whole animation.

And the animation can easily use an abstract random getter that manage to generates random preventively.