In C# you can use the Random class to get random numbers:
Random random = new Random(); var diceRoll = random.Next(1,7);
In the above we instantiate a new random and then get a number between 1 and 6, which is like a regular six-sided dice. You should reuse the same instance of
Random as it is seeded from the current system timestamp, if you do not reuse the
Random instance the numbers will likely be the same.
Random is only pseudo random, as the seed is easy to guess, if you want a secure random value based on OS entropy you can use RandomNumberGenerator (Derived from
RNGCryptoServiceProvider). If you are interested in the difference please read
Shivprasad Koirala answer on stack overflow - it is a really good read.
You can use
RandomNumberGenerator in the following way:
var diceRoll = RandomNumberGenerator.GetInt32(1,7);
That is it
I hope you enjoyed this short post, remember when you work with randomness you often want to control it somehow. Test doubles are great for this! As always - please leave a comment down below :)