EDN Admin
Well-known member
Hello,<br/><br/>I have a ID Generator class that I use to generate random numbers of varying lengths and values. I declare a random generator using a static variable as follows: <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small
<pre lang="x-c# private static Random rnd = new Random();[/code]
<br/><br/>I then have a method in the class that I call to create random strings, which Ive included below:<br/>
<div style="background-color:white;color:black
<pre> <span style="color:blue for (<span style="color:blue int i = 0; i < baseLength; i++)
{
<span style="color:blue int rndType = rnd.Next(1, 4);
<span style="color:blue if (rndType == 1 && allowNumbers)
{
str.Append(rnd.Next(0, 10));
}
<span style="color:blue else
{
<span style="color:blue int rndChar = rnd.Next(26);
<span style="color:blue if (rndType == 2)
{
str.Append(Convert.ToChar(<span style="color:#a31515 a + rndChar));
}
<span style="color:blue else
{
str.Append(Convert.ToChar(<span style="color:#a31515 A + rndChar));
}
}
}
[/code]
This code has worked great for the last year. Now, suddenly on one of my clients machines, the random.next() is starting to break, and after working fine for a few days, it suddenly starts to produce results equal to the lowest bounds I provide (either 0 or 1). Therefore, rnd.next(0, 10) produces 0 EVERY TIME. rnd.next(1, 4) produces 1 every time. rnd.next(26) produces 0 every time.<br/><br/>I dont know what could start causing this behavior. I declare my rnd variable once as a static variable outside of the random method I am calling, then re-use that static variable over and over. By not giving a seed value, I use the default seed generated by the clock, which is more than random enough for my purposes.<br/><br/>I cannot figure out for the life of me why rnd.next() suddenly stops working. Out of the blue! Fine one minute, then broken the next! Restarting the WWW service seems to fix the problem, but its a drastic approach to have to kill user sessions to do this.<br/><br/>Any ideas?<br/><br/>Mike
<span style="font-size:x-small
View the full article
<pre lang="x-c# private static Random rnd = new Random();[/code]
<br/><br/>I then have a method in the class that I call to create random strings, which Ive included below:<br/>
<div style="background-color:white;color:black
<pre> <span style="color:blue for (<span style="color:blue int i = 0; i < baseLength; i++)
{
<span style="color:blue int rndType = rnd.Next(1, 4);
<span style="color:blue if (rndType == 1 && allowNumbers)
{
str.Append(rnd.Next(0, 10));
}
<span style="color:blue else
{
<span style="color:blue int rndChar = rnd.Next(26);
<span style="color:blue if (rndType == 2)
{
str.Append(Convert.ToChar(<span style="color:#a31515 a + rndChar));
}
<span style="color:blue else
{
str.Append(Convert.ToChar(<span style="color:#a31515 A + rndChar));
}
}
}
[/code]
This code has worked great for the last year. Now, suddenly on one of my clients machines, the random.next() is starting to break, and after working fine for a few days, it suddenly starts to produce results equal to the lowest bounds I provide (either 0 or 1). Therefore, rnd.next(0, 10) produces 0 EVERY TIME. rnd.next(1, 4) produces 1 every time. rnd.next(26) produces 0 every time.<br/><br/>I dont know what could start causing this behavior. I declare my rnd variable once as a static variable outside of the random method I am calling, then re-use that static variable over and over. By not giving a seed value, I use the default seed generated by the clock, which is more than random enough for my purposes.<br/><br/>I cannot figure out for the life of me why rnd.next() suddenly stops working. Out of the blue! Fine one minute, then broken the next! Restarting the WWW service seems to fix the problem, but its a drastic approach to have to kill user sessions to do this.<br/><br/>Any ideas?<br/><br/>Mike
<span style="font-size:x-small
View the full article