When I run the following code in Mono (and not in Microsoft's C# apparently) in the monodevelop IDE. I get the same sequence of random numbers every time (00111111110110001001). If I change the range to something else e.g. r.Next(0, 5) then I do get random values. This seems very weird to me .. is this a bug or am I doing something dumb?
NOTE: it seems sometimes these numbers are negated.. e.g. I see 11000000001001110110 occasionally.
using System;
namespace TestRNG
{
class MainClass
{
public static void Main (string[] args)
{
Random r = new Random ();
for (int i = 0; i < 20; i++) {
Console.Write(r.Next(0, 2));
}
Console.WriteLine();
}
}
}
This is not the "Calling Next() on instances of Random called too closely together and therefore seeded with the same seed from the system clock problem".
Using the following code:
using System;
namespace TestRNG
{
class MainClass
{
public static void Main (string[] args)
{
Random r = new Random (Guid.NewGuid().ToByteArray()[0]);
for (int i = 0; i < 20; i++) {
int rn = r.Next ();
Console.WriteLine(rn + " " + (rn % 2));
}
Console.WriteLine();
}
}
}
I see that I get different values for rn every time but the sequence of values for rn % 2, which is what I suspect r.Next() is returning, is the same old values I always see. This leads me to believe that the Russian? fellow who posted an answer before and was down-voted to oblivion was correct (he's subsequently removed his answer). See http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx for example.
Based on one of the comments below this may only be a mono implementation thing.