Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate random number 58 bit length ? #20

Closed
Manosuper opened this issue Aug 30, 2023 · 5 comments
Closed

How to generate random number 58 bit length ? #20

Manosuper opened this issue Aug 30, 2023 · 5 comments

Comments

@Manosuper
Copy link

Manosuper commented Aug 30, 2023

Subj.

?

Thank you for your really fast random number generator.

;)

@lemire
Copy link
Owner

lemire commented Aug 30, 2023

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
Copy link

Hurd8x commented Aug 30, 2023

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
Copy link
Owner

lemire commented Aug 30, 2023

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 lemire closed this as completed Aug 30, 2023
@lemire
Copy link
Owner

lemire commented Aug 30, 2023

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
Copy link

Hurd8x commented Aug 30, 2023

Thank you so mach, Mister ))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants