lundi 16 août 2021

What's the fastest method in C for converting a 64bit random number into a smaller range?

Say, I have uint64_t rand = <some random number>, and char array[20] = .... My goal is to pick an element in array based on the content of rand.

One slow way is to use the remainder: size_t i = rand % 20 then pick the element by array[i].

Another way, which I guess is faster, is i = rand/UINT64_MAX * 20.

Is there any faster way on common hardware? E.g. laptop/desktop PCs?




Aucun commentaire:

Enregistrer un commentaire