Tracks
/
C#
C#
/
Syllabus
/
Randomness
Ra

Randomness in C#

5 exercises

About Randomness

In C# applications randomness is generally implemented using the System.Random class.

This article is an excellent introduction to the subject.

When becoming familiar with a library class it is always worth studying the documentation for properties and methods and their overloads.

The coding exercise highlighted a number of issues:

  • The random number generator does not require a seed.
  • Random.Next() can generate a range of integers
  • Random.Double() can generate a double between 0 and 1
  • The upper bounds in Next...(), Double() and Sample() are exclusive so that Random.Next(1,19) will yield values from 1 to 18 but not 19.
  • Once you have your random number you can do what you like with it

There are 3 patterns for implementing the last point:

  • You can get the random number and manipulate it. In the case of the coding exercise this would consist of calling Random.NextDouble() and multiplying by 100. This piece discusses making random selections from an array.
  • You can inherit from System.Random and override the Sample() method.
  • You can encapsulate an instance of System.Random as a member of a class of your own, for example to generate booleans.

The numbers generated by Random are not guaranteed to be unique.

It is recommended that you instantiate System.Random as a static member. But, note that it is not thread safe.

You may see documentation discouraging the use of seedless constructors. This mostly applies to .NET Framework rather than .NET Core (which is what Exercism uses). For the full story read this.

You are advised not to use System.Random for crypto or security. See this provider and this number generator.

Thread safety

When applied in the context of library APIs "not thread safe" is simply shorthand for saying that, if you are likely to access instances of the class through multiple concurrent threads, you should provide your own thread synchronization mechanisms or, in the case of collections, look at the possibility of using a concurrent version of the class. In the absence of these precautions, race conditions are likely to occur. If your code base does not use multiple threads, and is not likely to, then this warning is of little concern.

Edit via GitHub The link opens in a new window or tab

Learn Randomness

Practicing is locked

Unlock 4 more exercises to practice Randomness