Random numbers weird behavior

R

rlueneberg

I want to output a string of random integers with 20 rows and 20 cols
separated by comma.
The problem I am having is that it is always printing the same number
like below:


6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProjects
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

string str = string.Empty;

for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
Random r = new Random();
int RandomNumber = r.Next(0,20);
str = str + RandomNumber.ToString() + ",";
}
str = str + Environment.NewLine;
}

textBox1.Text = str;

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

This is ultimate desired result:

9,9,17,1,12,16,0,11,5,9,13,17,8,12,7,18,2,5,9,4,
4,19,3,14,18,1,16,16,11,15,19,3,14,8,12,3,7,11,15,6,
0,4,8,19,3,7,1,12,16,0,4,15,19,13,17,8,12,16,10,11,
5,9,13,4,8,3,6,17,1,5,9,0,15,19,2,14,17,1,16,7,
11,15,6,10,13,8,12,3,7,11,2,6,0,11,15,19,3,17,1,12,
16,0,11,15,9,13,4,8,12,16,7,1,5,9,0,4,8,2,6,17,
1,5,16,0,15,18,9,13,17,12,12,7,11,2,6,9,4,8,19,3,
7,10,2,16,0,11,15,19,3,17,8,12,16,7,11,15,9,13,4,8,
12,6,7,1,5,16,0,4,18,2,13,17,1,12,16,10,14,5,9,13,
17,8,3,6,10,1,5,9,4,15,19,3,6,18,1,16,0,11,15,19,
13,14,8,12,16,7,11,5,9,0,4,8,12,3,17,1,5,16,0,4,
15,9,13,17,1,12,16,10,1,5,9,13,4,8,2,6,10,1,5,0,
0,15,18,2,13,17,12,16,7,11,15,18,10,4,8,12,3,7,11,5,
16,0,4,8,19,3,17,8,12,16,0,14,15,9,13,4,8,12,6,10,
1,5,9,0,4,18,2,6,17,1,5,16,11,14,18,2,13,17,12,3,
7,10,14,5,9,4,8,19,3,7,1,2,16,0,4,15,19,13,17,8,
12,16,0,11,5,9,13,4,8,12,6,17,1,5,9,0,4,18,2,6,
17,3,11,2,6,10,1,5,19,3,14,18,2,16,17,11,15,19,3,14,
8,12,3,7,11,15,6,0,4,8,19,3,7,1,12,16,0,4,15,19,
13,17,8,12,16,7,11,6,10,1,5,8,3,7,18,2,6,9,1,15,

The funny thing is that if I run the code line by line in debug mode I
can make it work. Please help...

Rod
 
J

Jon Skeet [C# MVP]

I want to output a string of random integers with 20 rows and 20 cols
separated by comma.
The problem I am having is that it is always printing the same number
like below:

That's because you keep creating a new instance of Random, and each one
is being created with a seed of the current time. Because you're
creating so many of them in such a short space of time, they're all
getting the same seed.

You should reuse a single instance of Random, rather than creating a
new instance each time.
 
C

Carlos Manuel Perez Fernandez

Hello (e-mail address removed),
I want to output a string of random integers with 20 rows and 20 cols
separated by comma.
The problem I am having is that it is always printing the same number
like below:
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyProjects
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = string.Empty;

for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
Random r = new Random();
int RandomNumber = r.Next(0,20);
str = str + RandomNumber.ToString() + ",";
}
str = str + Environment.NewLine;
}
textBox1.Text = str;

}

private void Form1_Load(object sender, EventArgs e)
{
}
}
}
This is ultimate desired result:

9,9,17,1,12,16,0,11,5,9,13,17,8,12,7,18,2,5,9,4,
4,19,3,14,18,1,16,16,11,15,19,3,14,8,12,3,7,11,15,6,
0,4,8,19,3,7,1,12,16,0,4,15,19,13,17,8,12,16,10,11,
5,9,13,4,8,3,6,17,1,5,9,0,15,19,2,14,17,1,16,7,
11,15,6,10,13,8,12,3,7,11,2,6,0,11,15,19,3,17,1,12,
16,0,11,15,9,13,4,8,12,16,7,1,5,9,0,4,8,2,6,17,
1,5,16,0,15,18,9,13,17,12,12,7,11,2,6,9,4,8,19,3,
7,10,2,16,0,11,15,19,3,17,8,12,16,7,11,15,9,13,4,8,
12,6,7,1,5,16,0,4,18,2,13,17,1,12,16,10,14,5,9,13,
17,8,3,6,10,1,5,9,4,15,19,3,6,18,1,16,0,11,15,19,
13,14,8,12,16,7,11,5,9,0,4,8,12,3,17,1,5,16,0,4,
15,9,13,17,1,12,16,10,1,5,9,13,4,8,2,6,10,1,5,0,
0,15,18,2,13,17,12,16,7,11,15,18,10,4,8,12,3,7,11,5,
16,0,4,8,19,3,17,8,12,16,0,14,15,9,13,4,8,12,6,10,
1,5,9,0,4,18,2,6,17,1,5,16,11,14,18,2,13,17,12,3,
7,10,14,5,9,4,8,19,3,7,1,2,16,0,4,15,19,13,17,8,
12,16,0,11,5,9,13,4,8,12,6,17,1,5,9,0,4,18,2,6,
17,3,11,2,6,10,1,5,19,3,14,18,2,16,17,11,15,19,3,14,
8,12,3,7,11,15,6,0,4,8,19,3,7,1,12,16,0,4,15,19,
13,17,8,12,16,7,11,6,10,1,5,8,3,7,18,2,6,9,1,15,

The funny thing is that if I run the code line by line in debug mode I
can make it work. Please help...

Rod


The problem is that Random returns a random number based on the system clock.
If your calls to Next() are very close one to another, the clock will have
advanced only slightly and you end up with a larga number of equal or very
similar results.

One solution I found is using Thread.Sleep(milliSeconds). Doing this will
(obviously) make your code slower, but it will stop the thread so the clock
has time to advance and you results will be more diverse. Play a little with
the required parameter so you fine-tune slowness vs. randomness...

Regards,
Carlos M Pere
 
L

Larry Lard

Carlos said:
Hello (e-mail address removed),
I want to output a string of random integers with 20 rows and 20 cols
separated by comma.
The problem I am having is that it is always printing the same number
like below:
[snip]
The funny thing is that if I run the code line by line in debug mode I
can make it work. Please help...

Rod


The problem is that Random returns a random number based on the system
clock. If your calls to Next() are very close one to another, the clock
will have advanced only slightly and you end up with a larga number of
equal or very similar results.

No, this is incorrect. For a given Random in a particular state, the
next value returned is not time dependent. What *is* time dependent is
the initial state given to a new Random when no seed is
explicitlysupplied, and that's the problem the OP is having - by
*creating* a new Random every time, at pretty much the same instant, you
end up with 'the same' Random each time, which is why the values are all
the same.
One solution I found is using Thread.Sleep(milliSeconds). Doing this
will (obviously) make your code slower, but it will stop the thread so
the clock has time to advance and you results will be more diverse. Play
a little with the required parameter so you fine-tune slowness vs.
randomness...

The correct thing to do is to create just one Random then ask it for its
Next values - the program will run just as fast, but will actually give
different values.

The reason running in debug 'makes it work' is that each created Random
is created at a different time, so has a different state, so has a
different first value.
 
Top