jeudi 19 septembre 2019

Is this a correct way to get a random number between 1024 and 65535?

unpack('n', random_bytes(2))[1] % 64512 + 1024;

I believe random_bytes(2) returns a binary between 0-65535, unpack('n') creates a decimal out of it, the reminder after dividing by 64512 is between 0-64511, adding 1024 results in 1024-65535. Anything I missed? (And no, mt_rand(1024, 655535) is not adequate for the purpose.)




Aucun commentaire:

Enregistrer un commentaire