-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Description
Describe the issue:
Both the functions random_f and random_standard_cauchy mentioned in numpy/random/src/distributions/distribution.c have a returned statement of the form f1()/f2().
since C doesn't guarantee the order of evaluation of its operands, either of f1() or f2() can be evaluated. The problem happens when f1() and f2() are the same function and they evaluate results based on the state of one of their arguments. Let's say both f1 and f2 are same function f as follows,
typedef struct {
int call;
} bit_state;
double f(bit_state){
if (bit_state.call == 0){
return 8;
bit_state.call++;
}
else{
return 2;
}
}
double some_func(bit_state){
return f(bit_state)/f(bit_state)
}
if the second f() is evaluated first here the output is reciprocal of that when first f() is evaluated. The same happens in random_f and random_standard_cauchy.
double random_f(bitgen_t *bitgen_state, double dfnum, double dfden) {
return ((random_chisquare(bitgen_state, dfnum) * dfden) /
(random_chisquare(bitgen_state, dfden) * dfnum));
}
evaluation of random_chisquare depends on bitgen_state(see random_chisquare for more details).
I have provided the bug fix for the same in the PR https://github.com/numpy/numpy/pull/29598/files
Reproduce the code example:
import numpy as np
from numpy.random import Generator, MT19937
random = Generator(MT19937(1234567890))
actual = random.f(12, 77, size(3,2))
print(actual)
Error message:
Python and NumPy Versions:
numpy latest version 2.3 is affected.
Runtime Environment:
No response
Context for the issue:
No response