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

Non-random data is returned if any exceptions are raised while generating random bits #389

Open
cbryant203 opened this issue Oct 3, 2024 · 1 comment

Comments

@cbryant203
Copy link

If the procedure which generates random data raises an exception, this cause an error message to be printed, but this does not propagate, so the original function returns non-random data. Note that the exception does trigger this message:

Exception ignored on calling ctypes callback function: <cyfunction raw_64_to_double.<locals>.f at 0x7f4f426e1d80>

which pretty much explains why this happens. Attached is a trivial randomness generator which always raises an exception, and a tester which invokes it asking for 10 random values. It prints these values:

[6.89921293e-310 6.89921293e-310 6.89921293e-310 6.89921293e-310
 6.89921293e-310 6.89921293e-310 6.89921293e-310 6.89921293e-310
 6.89921293e-310 6.89921293e-310]

which are obviously not random!

dummydemo.txt
dummyrand.txt

I ran this with numpy==2.1.1 randomgen==2.0.1 but it's not version-specific

@bashtage
Copy link
Owner

bashtage commented Oct 3, 2024

Once the PRNG is running there is no exception handling. This is done for performance since within the core PRNGs one doesn't expect to encounter actual exceptions (there is plenty around, e.g., constructing arrays to hold random values).

I would say this is by design. If you some reason you need to check for exceptions in the low level interface then you will need to call PyErr_Occurred and handle the exception yourself. There is a tiny bit of the in RDRAND which can raise errors if the bit pool available to the instruction is empty. Some explanation is here https://bashtage.github.io/randomgen/bit_generators/rdrand.html

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

2 participants