How to generate a random System.Decimal
between a given min
and max
(both inclusively) under the following conditions:
- The distribution should either be uniform w.r.t. the finite number of representable decimals (i.e., each bit pattern representing numbers between min and max is equally likely) OR uniform w.r.t. the continuous number line (i.e., each interval between min and max is equally likely to be hit).
- The probability for each representable number between min and max should be greater than 0, i.e., the algorithm must not target a subset of the decimal range only.
(I've already read the number one question on the topic here on SO Generating a Random Decimal in C#, also read various articles on the web about decimals, including Jon Skeet's and the .NET docs - they couldn't help me so far)
My current approach is similar to my algorithm for generating a random float/double: I want to somehow get the count of representable bit patterns between min and max (see also my question here), then generate a random number between 0 and count, then map this number back to the bit patterns somehow. Unfortunately, I'm not sure whether the last part of my algorithm is "possible" (i.e., without really enumerating each possible bit pattern between the two).
I hope you guys have some kind of good idea how to do that in a much better way.
Aucun commentaire:
Enregistrer un commentaire