Groups | Blog | Home
all groups > dotnet windows forms > january 2007 >

dotnet windows forms : Random numbers weird behavior


rlueneberg NO[at]SPAM gmail.com
1/28/2007 4:22:55 AM
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
Jon Skeet [C# MVP]
1/28/2007 12:38:59 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Carlos Manuel Perez Fernandez
1/29/2007 9:27:58 AM
Hello rlueneberg@gmail.com,

[quoted text, click to view]


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


Larry Lard
1/29/2007 2:21:24 PM
[quoted text, click to view]
[snip]

[quoted text, click to view]

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.

[quoted text, click to view]

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.



--
Larry Lard
larrylard@googlemail.com
The address is real, but unread - please reply to the group
Carlos Manuel Perez Fernandez
1/30/2007 8:48:18 AM
Hello Larry,

I stand corrected. Thanks for the lesson. =)

Regards,
Carlos M Perez
http://www.picacodigos.com
mailto:carlosm.perez@gmail.com


AddThis Social Bookmark Button