lemire/fastrand

How to generate random number 58 bit length ?

Closed this issue · 5 comments

Subj.

?

Thank you for your really fast random number generator.

;)

lemire commented

Can you elaborate ?

This library is not really about number generation per se, but rather about a faster replacement for random.randint.

Please describe what you are trying to do.

Hurd8x commented

I try to generate binary number's in length from 68 to 120 bits(0 and 1).

random.randint is too slow for me, and not get result.

I compare all generated numbers for ex,thith this 60 bit lenght number:

0b010110010001111011101000010000011101011010111110101010111100

And then I find number, code break.

???

Thank you very mach for your answers !!

lemire commented

So call fastrand.xorshift128plus() & 0x3ffffffffffffff to get a 58-bit random number. For a 59-bit number, do fastrand.xorshift128plus() & 0x7ffffffffffffff. It should be fast:

 $ python3 -m timeit -s 'import random' 'random.randint(0,0x3ffffffffffffff)'
1000000 loops, best of 5: 371 nsec per loop
$ python3 -m timeit -s 'import fastrand' 'fastrand.xorshift128plus() & 0x3ffffffffffffff'
5000000 loops, best of 5: 42 nsec per loop
lemire commented

If you need a 120 bits, call fastrand.xorshift128plus() to get a 64-bit number, then call fastrand.xorshift128plus() & 0xffffffffffffff to get the remaining 56 bits.

Hurd8x commented

Thank you so mach, Mister ))