lemire / fastrand

Fast random number generation in an interval in Python: Up to 10x faster than random.randint.
Apache License 2.0
88 stars 12 forks source link

How to generate random number 58 bit length ? #20

Closed Manosuper closed 1 year ago

Manosuper commented 1 year ago

Subj.

?

Thank you for your really fast random number generator.

;)

lemire commented 1 year ago

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 1 year ago

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 1 year ago

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 1 year ago

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 1 year ago

Thank you so mach, Mister ))