-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
math/rand: Float32() and Float64() are not uniformly distributed #4965
Labels
Milestone
Comments
The returned distribution is the projection of the (supposedly uniform) distribution returned by the Source to the non-constant-step realm of floating-point numbers. It means that may look different near zero and near one, but that doesn't make it non-uniform. It is even a closer approximation to a mathematical uniform distribution than your proposal, which makes output more regularly spaced but less uniform. |
First, let me say I'm not an expert and I'm glad for corrections. So far, I'm getting that you are right and my title for the issue might have been wrong. In laymans terms, the closer to 1.0 we get, the more numbers get "ceiled"and "floored", but as the same number of ceils and floors take place, it all evens out. I still think my proposed approach is better. Say you want normal distributed numbers. Usually, you'd take uniform distributed numbers and convert them. The current approach would skews the tails. When you say the current approach is more uniform, it may well be - but not in a "fair" way. AFAIK regular spacing beats higher resolution near zero - at least for simulations it is less surprising and more fair, which is a good thing. |
I read up on it some more and stand my ground: it's not uniform distributed. According to Wikipedia, each value in a uniform distribution has the same probability to "get picked", the probability is 1 / (max - min). That's clearly not the case for the current implementation, where picking the value closest to 1.0 is 512 times as probable as picking 0.0. Please ignore my previous comment. Sources: http://en.wikipedia.org/wiki/Uniform_distribution_(continuous)#Probability_density_function and http://en.wikipedia.org/wiki/Probability_density_function |
You are confusing uniform distribution on discrete sets and the continuous uniform distribution. The result of rand.Floatxx is certainly not uniformly distributed over a discrete set of floating-point numbers, and there is no point into doing that unless you have a particular reason. However, it is certainly a good approximation of the continuous uniform distribution over [0,1]. If you think it is not the case, please show a problem that has a good reason to use rand.Floatxx, that is not correctly addressed by the current implementation, and would be better solved by your more complicated proposal. People who want samples of a discrete distribution should definitely use integers, not floating-point numbers. |
I yield, I cannot think of a problematic scenario. After running the attached benchmark with the current approach, another version multiplying with a constant factor instead of dividing, an improved version of my second proposal and my first proposal, the current implementation won (if only by 65 ns for 2000000000 ops vs 67 ns). The only argument left to me is that maybe it could ease fixing issue #4254 if source.Uint64() instead of Int63() were used because the conversion code wouldn't have to be adapted - but I guess that's neither dramatic nor realistic. I really love this language and had high hopes to contribute something, but apparently I'll have to search elsewhere. Keep up the great work and thank you for the time! Attachments:
|
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by arnehormann:
The text was updated successfully, but these errors were encountered: