1

I've used .NET's Random class in a few projects, but one thing I often wonder is why it was designed as an instance object, rather than having static methods which return random values.

For example:

Random rnd = new Random()
int x = rnd.Next(0, 255);

Is there any reason, or design choice as to why it wasn't designed with both instance and static methods so that I could just do this:

int x = Random.Next(0, 255);

Is there any reason this functionality wasn't implemented, as I think it would be useful?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • Right. Sometimes you might want "give me random numbers starting from this specific seed" but most the time you want "give me random numbers as random as you can". – Colonel Panic Jan 28 '13 at 15:31
  • 1
    It's highly doubtful that the person (or persons) that designed `Random` will chime in here, leaving people to *guess* and an answer (i.e. not based on facts). Static methods need to be written to support multi-threaded as well as re-entrancy. It's quite likely that you'd want two threads to generate random numbers independently--something you couldn't really do with "global" static methods. – Peter Ritchie Jan 28 '13 at 15:39
  • I agree that a combination of instance and static methods would have been a useful direction to take. Sometimes you want a bit of one-off randomness without having to worry about managing the RNG explicitly. I created [this NuGet package](https://github.com/madelson/MedallionUtilities/tree/master/MedallionRandom#static-random-apis) which contains, among other things, static convenience methods such as `Rand.NextDouble()` (like `Math.random()` in Java) and `Rand.Current` (thread-safe static `Random` instance). – ChaseMedallion May 07 '16 at 13:37

3 Answers3

2

System.Random has two constructors, a bare one and one that accepts a seed (the basis for any random numbers generated).

If this was a static method, then you'd either need to pass the seed every time or allow some kind of default.

You might want to check this SO question as to why the seed really matters.

How do I seed a random class to avoid getting duplicate random values

Community
  • 1
  • 1
Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
  • This would only be an issue when when the class provide _only_ static members. The suggested addition could easily use a clock-base seed. – H H Jan 28 '13 at 15:36
0

Random as an instance object allow you to configure the Randomgenerator;

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.

This can become quite handy when for instance testing

See Random Constructor (Int32)

lboshuizen
  • 2,746
  • 17
  • 20
0

If there were a static Random.Next method as you suggest, it would have to be designed such that multiple threads calling it concurrently wouldn't corrupt the state. The current Random class does not support multiple concurrent users.

Also, if there was only one Random instance, you couldn't have two parts of your code that depend on different random sequences. I ran into that problem many times when coding in C, which had a global random number generator. I ended up having to write my own per-instance generator.

Making the random number generator an instance class rather than a static gives you more flexibility. If you just want one global in your program, declare it at global scope and use it, just as you would if there was just one static Random object. It ends up being one extra line of code.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351