I need to create a block of unique lines to test a different project I am working on.
So I created a simple program to generate a random string of X length.
The issue is that if I call it once, I get a random string, if I call it again (in a for loop for example) I get the same string for the entire execution of the loop.
I have a feeling that it's being cached or something but I didn't know .net did that and I am just confused at this point.
calling code:
StreamWriter SW = new StreamWriter("c:\\test.txt");
int x = 100;
while (x >0)
{
SW.WriteLine(RandomString(20));
x--;
}
here is the method:
private static string RandomString(int Length)
{
StringBuilder sb = new StringBuilder();
Random randomNumber = new Random();
for (int i = 0; i <= Length; ++i)
{
int x = randomNumber.Next(65, 122);
sb.Append(Convert.ToChar(x));
}
return sb.ToString();
}
and here is the output:
"VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
..................
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB
VEWMCQ`Fw]TvSFQawYnoB"
So what gives i thought Random.next() would always return a new random number?
You are creating the
Random
instances too close in time. Each instance is initialised using the system clock, and as the clock haven't changed you get the same sequence of random numbers over and over.Create a single instance of the
Random
class and use it over and over.Use the
using
keyword so that theStreamWriter
is closed and disposed when you are done with it. The code for a loop is easier to recognise if you use thefor
keyword.The method takes the
Random
object as a parameter.Also, use the length to initialise the StringBuilder with the correct capacity, so that it doesn't have to reallocate during the loop. Use the < operator instead of <= in the loop, otherwise you will create a string that is one character longer than the
length
parameter specifies.Because you create a new Random object in each call.
Simply move the randomNumber out of the method and make it a class member.
All software Random generators are 'pseudo random' , they produce a sequence of numbers bases on a (starting) seed. With the same seed they produce the same sequence. Sometimes this is usefull. If you want to produce your program to produce the same sequence on each run, you can use
new Random(0)
.Edit: apparently the .Net Random class is auto-seeding, I didn't know that. So it is a timing problem, as others have pointed out.
only declare randomNumber once
Similar question, lots of answers:
random string generation - two generated one after another give same results
the seed for the random numbers are all the same due to the short amount of time it takes, in effect you recreate the random generator with the same seed every time, so the Next() call returns the same random value.
See Random constructor description at MSN, this part:
So either call the Random() constructor only once at the beginning of your program or use the Random(int32) constructor and define a varying seed yourself.