Why isn't the Microsoft built in class Random class an type static class as the Math. class is.
Why didn't they make this class static?

Recommended Answers

All 3 Replies

You may want more than one random number generator so that you have more than one sequence at a time.

Also, if it were static, you'd have to include code to make it thread safe across the entire system.

Random rObj =new Random();
int n= rObj.next(10);

Random number generators, in a lot of cases, need to be deterministic and you often need more than one in order for it to remain deterministic.

If the RNG were static, you would be limited to one. Due to threading, this means that the RNG may not be deterministic. For example; if two threads run within nanoseconds of each other, different executions may end up getting the numbers switched around, even in a "deterministic" scenario.

Additionally, there is more than one type of RNG. The standard RNG is unsuitable for cryptography, so there is a special one for that purpose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.