Using Random.NextDouble() method

Bvwalker1

New member
Joined
Oct 16, 2021
Messages
3
Programming Experience
1-3
Since this returns a number between 0 and 1 (excluding 1 itself), my question is how can use this to represent a percentage. For example, if I choose a variable to be 0.75 and have it compare to the RNG, would there be a 25% of the result being higher? It seems to me it would be slightly less because it cannot return a value of 1. Or, maybe I would need to say there would be a 25% chance of it being equal to or greater than 0.75?
 
It has high precision and goes to 0.99999999999999978, not just hundreds 0.00, 0.01 .... 0.99.
 
My mind went elsewhere regarding the slight skewing of probabilities that happens as the result of using division to compute a random floating number. I'm glad @JohnH got to the heart of the matter.

Anyway, of interest to some people would be:


For most practical purposes, it shouldn't matter, but if you are into deep numerical analysis and need truly evenly distributed values, then it would make a difference between comparing <[icode] vs. [icode]<=, as well as, switching to a different random number generator, or at least a different way of getting a random floating point number. (See reference source to see that C# uses the division method to a random floating point number.)
 
Back
Top Bottom