lemire / fastrand

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

OverflowError when generating random int for large numbers #21

Closed suokunlong closed 3 months ago

suokunlong commented 3 months ago

Steps to Reproduce:

import fastrand

small = 0x1
large = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
fastrand.pcg32randint(large, small)

Result: OverflowError: Python int too large to convert to C long.

import random

small = 0x1
large = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
random.randint(small, large)

Result: 103122144310615264154150719382125058357621691471641913955502053264321705091936

lemire commented 3 months ago

That is the expected result. The pcg32 generator is a 32-bit generator, so it generates values within the 32-bit range.

lemire commented 3 months ago

Version 2.0.0 released just now adds support for the 64-bit case (on some platforms) but if you need arbitrarily large integer support, it is not the right library.

suokunlong commented 2 months ago

Do you have any idea of any fast random generator for big int (up to 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141)?

Random.randint() (and random.randrange()) is very slow - it takes 20+ seconds to generate 10,000,000 random ints.

lemire commented 2 months ago

Do you have any idea of any fast random generator for big int

Currently, I do not.

What is the use case? Why do you do this?