This article is currently in the process of being translated into Spanish (~32% done).
Randomness with the Random class
En algún momento, es probable que necesites lograr aleatoriedad en tu aplicación. Las razones pueden ser muchas: tal vez necesites ordenar los elementos de una lista en un orden aleatorio (barajar) o quizás estés en el proceso de crear un juego y quieras que el oponente se mueva en una dirección aleatoria. No importa cuál sea la razón, necesitas alguna forma de obtener un valor aleatorio del ordenador, y afortunadamente, C# puede ayudarnos con esto.
En la mayoría de casos comienza con la clase Random, la cual te permitirá generar uno o varios números dentro un rango específico, es bastante sencillo de usar:
Random random = new Random();
Console.WriteLine("A random number: " + random.Next());
Simplemente inicializamos una nueva instancia de la clase Random y a continuación llamamos al método Next() para obtener un número aleatorio (int) - así de fácil.
Si lo que necesitas es un número con decimales en vez de un entero puedes usar en vez el métodod NextDouble(). Esto te dará un número entre 0.0 (incluido) y 0.99 (realmente cualquier número menor a 1.0)
Random random = new Random();
Console.WriteLine("A random number with decimals: " + random.NextDouble());
A Random range
En la mayoría de ocasiones, no quieres cualquier número aleatorio, lo quieres dentro de algún rango, por ejemplo entre 1 y 100. No te preocupes, la clase Random hace eso muy fácilmente,
Random random = new Random();
Console.WriteLine("A random number between 1 and 100: " + random.Next(1, 101));
I simply change the Next() call to one that includes a minimum and a maximum number. Notice that while the minimum value is inclusive (this number is included in the possible outcome), the maximum value is exclusive (this number is NOT included in the possible outcome), which is why I use 101 instead of 100. If you wanted the number to between 0 and 99, it would instead look like this:
Console.WriteLine("A random number between 0 and 99: " + random.Next(0, 100));
Seeding the Random class
The Random class is always instantiated with a seed, either directly if you supply one, or indirectly by the framework. This seed allows you to basically control the randomness - a specific seed used multiple times will allow you to create the same set of random numbers. This may sound strange, but it can be useful for testing specific scenarios in your application/game.
A seed is supplied to the Random class by using the constructor overload like this:
Random random = new Random(1000);
Let's see what happens if we use this to generate 5 random numbers after eachother:
Random random = new Random(1000);
for(int i = 0; i < 5; i++)
Console.WriteLine("A random number between 1 and 100: " + random.Next(1, 101));
No matter how many times you run this code, I'm pretty sure that the 5 "random" numbers you get will look like this:
A random number between 1 and 100: 16
A random number between 1 and 100: 24
A random number between 1 and 100: 76
A random number between 1 and 100: 1
A random number between 1 and 100: 70
So as you can see, the random values created by the Random class are not that random after all. Instead, they use the seed value to create pseudo-random values. Therefore, when a seed is not supplied by you, the framework will create one for you. In the original .NET framework, the current time is used, while the more modern .NET core framework will use the thread-static, pseudo-random number generator, to generate a seed. So in other words, unless you need controlled randomness, you can simply stick with the default constructor of the Random class, as used in the first examples.
Summary
Use the Random class to generate a random number, but unless you want it to be predictably random, a random seed needs to be used - the framework will take care of this for you, unless you specifically supply a seed through the constructor overload.
Also be aware that instantiating the Random class is somewhat expensive, so don't do this multiple times inside a loop - instead, just instantiate a single Random instance and call the Next() method as many times as you need to.
Lastly, as we already discussed, the Random class is a pseudo-random generator. If you need something that's more secure, e.g. for cryptography and/or random password generation, you can use the RNGCryptoServiceProvider class.