get a uniformly distributed random number in csharp

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.cs
Random random = new Random();
double min = 0.0;
double max = 1.0;
double value = min + (random.NextDouble() * (max - min));
124 chars
5 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