The error message tells you precisely the problem. Random
is a class. Next
is a non-static method. You need an instance, or object reference, of the class in order to use that method.
var random = new Random();
// use random.Next(upperLimit);
You should note that if you are using random
in a tight loop, you would want to create the instance outside the loop and reuse it, or at an otherwise higher level (such as a member field of a class). The way the class is seeded, successive instances will generate the same "random" sequences of values. This is a common pit that people have fallen into.
You should also be aware that based upon your usage where you are getting an integer from 0 to 99, casting to double, and dividing by 100... there's a more straightforward approach. Simply use NextDouble()
, which gives a value greater than or equal to 0 and less than 1.0.
double d = random.NextDouble();