What is the easiest way to generate quasi random numbers in C#?
c#, random
Solution
public class MathHelpers
{
private static Random random = new Random();
public static int GetRandomNumber(int min, int max)
{
return random.Next(min, max);
}
}
This way, you're not creating a new Random object every time, rather you're reusing the same one. When you recreate a new one quickly enough, they will yield the same results. If however, you reuse an existing one, you'll get randomness.
Problem
All I want is a pragmatic random number generator in C# so I can say e.g. ``` int dummyAge = MathHelpers.GetRandomNumber(20,70); ``` and have it seem quasi random, e.g. to generate dummy data. Most stack overflow questions on this topic and on the web get into a philosophical discussions on true randomness which is not what I'm interested at the moment, e.g. I did one in PHP a long time ago which uses milliseconds/sleep which is fine for dummy data, I'm just trying to do this in C# quick. Does anyone have a quick half-decent C# random number generator based on some time seed, etc. or, how could I change the following code so that it always doesn't generate the same 5 number in a row? ``` using System; namespace TestRandom23874 { class Program { static void Main(string[] args) { Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10)); Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10)); Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10)); Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10)); Console.WriteLine("the random number is: {0}", MathHelpers.GetRandomNumber(1, 10)); Console.ReadLine(); } } public class MathHelpers { public static int GetRandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } } } ```