-
Notifications
You must be signed in to change notification settings - Fork 22
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
Bugs in random #5
Comments
(Note these are just performance bugs and a bug that isn't triggered in your code... well I'm pretty sure |
Nice finds, @Sc00bz! I'm looking at this code for the first time as well. It looks like we intended to switch to uint32 but didn't quite get there as you noticed. So I was going to go with option 3: rename and fix max. But then I found out why this was originally an int32 and not a uint32. In order to fix max, I need something like I looked at your implementation to see how you solved that, and I don't think you did. You're doing |
Best thing I could come up with is |
I'm an idiot. Yours is perfectly fine when |
randomInt32()
gets 8 bytes of random for a 32 bit int.spg/util.go
Line 44 in 5a36bf7
int31n()
gets a random uint32 [0, 2**32). Whenn
is not a power of 2,max
is at most 2**31-2. This has a >25% to <50% vs a >50% to <100% chance of success each loop. To fix this mask the randomv := randomInt32() & math.MaxInt32
.spg/util.go
Lines 82 to 95 in 5a36bf7
int31n()
is called withn
> 2**31 it is not uniform. This bug was caused by switchingn
from int32 to uint32. Switchn
back, doif int32(n) <= 0 { panic...
, or rename touint32n()
and fixmax
(see https://github.com/Sc00bz/ModRandom/blob/a2cd9247a0dcb0183ec6305574b5696f51186540/csprng-cpp/csprng.cpp#L259-L284).The text was updated successfully, but these errors were encountered: