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
Random coin sort is slightly biased #907
Comments
Interesting, good eye! Looks to me like the default for initializing the coin selection in But I think you are right, if the Well, Were you noticing nay other bugs? Out of curiosity, What brings you own to this part of the code base? Thank you for your observation! |
Floats are not uniformly distributed in that range, so I don't think the probability is that simple. But like I mentioned above, I don't think "bug" is of any consequence :) |
Maybe https://github.com/bcoin-org/bcrypto/blob/master/src/random/random.c |
If you want to go that route, then a random bitflip is probably better, eg. |
@tynes Actually that function you linked doesn't allow you to generate the full uint32 range, because the max is exclusive but also asserted to be a valid 32 bit uint itself. Don't know if this is on purpose :) |
Random sorting of coins during coin selection uses
Math.random()
, which outputs in the interval[0, 1[
. Splitting this interval in two equal intervals yields[0, 0.5[
and[0.5, 1[
. This means the current sort is slightly biased as it considers the intervals[0, 0.5]
and]0.5, 1[
. In large this probably doesn't matter, but it is a slight logic bug. It can easily be fixed by changing to either<
or>=
bcoin/lib/primitives/mtx.js
Line 2040 in 7bc3251
The text was updated successfully, but these errors were encountered: