Microsoft
Software
Hardware
Network
Question : C# random number question
hi experts,
I wrote the following code with expectation that it will generate 100 random numbers evenly distributed from -100 to 100. However, it produced blocks of same numbers, examples of the screenshots are attached as JPG. I attached two screens.
Can anyone explain why it would produce results like this? If it is the Random() fault, as we all know a lot of microsoft method/functions don't generate real statistical randoms, how can we fix it?
Thanks you for your attention.
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//////////
////
for (int i = 0; i < 100; i++)
{
Random randomNumber = new Random();
int n1 = randomNumber.Next(-100, 100);
int n2 = Math.Abs(n1);
Console.WriteLine("origina
l={0}, and abs={1}", n1, n2);
}
//////////////////////////
//////////
//////////
//////////
//////////
////////
Attachments:
randomeNumberQuestion1.JPG
(71 KB)
(File Type Details)
random1
randomeNumberQuestion2.JPG
(84 KB)
(File Type Details)
random2
Answer : C# random number question
Add a sleep()
for ( int i = 0; i < 100; i++ )
{
System.Threading.Thread.Sl
eep( 200 );
Random r = new Random( (int)DateTime.Now.Ticks );
int n1 = r.Next( -100, 100 );
int n2 = Math.Abs( n1 );
// Console.WriteLine() here.
}
Random Solutions
WSUS 3.0 SP2 Installation Problem
Free replacement to DynIP?
Java Programing
Monitor a Ou and all sub Ou's and email if a computer has not contacted domain for more than 10 days. Vbs or Powershell
Adobe runtime error
After every restart I have to re-enter the gateway IP address (Win7)
Can I send a cookies' data to a server side database without reloading the page?
Network login and C$ for compter accounts on the domain
Type of Cluster?
Outlook 2007 Freezez up during Spell Check