Home All Groups Group Topic Archive Search About

Random numbers weird behavior

Author
28 Jan 2007 12:22 PM
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
Author
28 Jan 2007 12:38 PM
Jon Skeet [C# MVP]
<rlueneb***@gmail.com> wrote:
> 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
29 Jan 2007 9:27 AM
Carlos Manuel Perez Fernandez
Hello rlueneb***@gmail.com,

Show quoteHide quote
> 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 Perez
Author
29 Jan 2007 2:21 PM
Larry Lard
Carlos Manuel Perez Fernandez wrote:
Show quoteHide quote
> Hello rlueneb***@gmail.com,
>
>> 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.



--
Larry Lard
larryl***@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Author
30 Jan 2007 8:48 AM
Carlos Manuel Perez Fernandez
Hello Larry,

I stand corrected. Thanks for the lesson. =)

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

Bookmark and Share