To generate a uniformly distributed random number in C#, you can use the Random
class, which provides a Next
method to generate random integers within a specified range. To get a uniformly distributed random number within a floating-point range, you can divide the random integer by the maximum integer value and then multiply it by the range. Here's an example:
main.cs124 chars5 lines
In this example, min
and max
represent the bounds of the range, and NextDouble
generates a random double between 0.0 and 1.0 (exclusive), which is then scaled to the desired range by multiplying by (max - min)
and adding min
.
Note that this method generates a continuous, uniform distribution, meaning that any value within the range has an equal probability of being generated.
gistlibby LogSnag