I'm working in Microsoft Visual C# 2008 Express.
I found this snippet of code:
public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
the problem is that I've run it more than 100 times, and it's ALWAYS giving me the same answer when my min = 0 and max = 1. I get 0 every single time. (I created a test function to run it - really - I'm getting 0 each time). I'm having a hard time believing that's a coincidence... is there something else I can do to examine or test this? (I did re-run the test with min = 0 and max = 10 and the first 50ish times, the result was always "5", the 2nd 50ish times, the result was always "9".
?? I need something a little more consistently random...
-Adeena
As others have mentioned, the Random being built multiple times per second uses the same second as the seed, so I'd put the Random constructor outside your loop, and pass it as a parameter, like this:
Also as mentioned by others, the max is exclusive, so if you want a 0 or 1, you should use [0,2] as your [min,max], or some larger max and then do a binary AND with 1.