Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hello...

 

I'm creating a loop which inside of it have a "while" keyword that loops as many times until the "given" number is randomly generated, the problem is, it always gives me the same number (I know this because i print everyloop to a rich text box)! But, if i make a break point inside the "while" and press F11 I can see it change. Yet, If i put "this.Refresh()" after the "...rRandom.NextDouble..." it works great too... why? The code moves faster then then random calculation???

 

Is there any other a way to skip the use of "this.Refresh()" because is slows things down!

 

Ok, here is an example of what what the rich text box displays when executing the code with no .refresh() keyword : (it is suposed to stop when a 10 is generated)

 

2

2

2

2

2

2

2

2

 

Now, I will execute it again with no code change or without stoping the program

 

7

7

7

7

7

7

 

Now with the .refresh() (works fine here)

 

4

2

3

3

6

8

10

 

tks for any future help...

 

PS: Is it possible to force a var. to refresh its value?

Posted (edited)
can you post a bit more code...

 

foreach (System.Windows.Forms.Control frmCtrl in pnl_right.Controls)
{	
if (frmCtrl.Name.StartsWith("frm") == true)
	{
	foreach (System.Windows.Forms.Control ckbCtrl in frmCtrl.Controls)
		{
		if (ckbCtrl.Name.StartsWith("ckb") == true)
			{
			System.Windows.Forms.CheckBox tempCtrl = (System.Windows.Forms.CheckBox)ckbCtrl;
			if (tempCtrl.Checked == true)
				{
				int iTemp = 0;
				Random rRandom = new Random();

				//fica em loop enquando a var. iTemp for um número par
				while (iTemp == 0 && iTemp == 2 && iTemp == 4 && iTemp == 6 && iTemp == 8)
					{
					iTemp = (int)(rRandom.NextDouble() * 9);
					}
				sImageMapName[Convert.ToInt16(tempCtrl.Tag)] = iTemp + tempCtrl.Name.Remove(0,4);
				i++;
				}
			else
				{
				int iTemp = 1;
				Random rRandom = new Random();

				//fica em loop enquando a var. iTemp for um número ímpar
				while (iTemp == 1 | iTemp == 3 | iTemp == 5 | iTemp == 7 | iTemp == 9)
					{
					iTemp = (int)(rRandom.NextDouble() * 9);
					}
				this.Refresh();
				sImageMapName[Convert.ToInt16(tempCtrl.Tag)] = iTemp.ToString() + tempCtrl.Name.Remove(0,4);
				i++;
				}
			}
		}
	}
}
for (i = 0; i < 50; i++)
	{
	this.richTextBox1.Visible = true;
	this.richTextBox1.Text += sImageMapName[i] + " " + i + "\n";
	}
}

Edited by PlausiblyDamp
Posted

Didnt try running your code but the fact that you create the Random generator several times whithin the foreach loops gave me this idea:

 

All normal 'random' generators like Random use the date & time and some other stuff to seed the random sequence. They do some calculations with all the input parameters and get a nice sequence of numbers that is relativly random.

 

Your loop however is so fast that the internal clock has no chance to update meaning that the seed is the same each time the Random object is created. This gives the same 'random' number each time. That is probably why there is a separate random generator in the system.security.cryptography namespace.

 

The easiest solution however, is to just create the random object 1 time outside the first foreach (System.Windows.Forms.Control frmCtrl in pnl_right.Controls) loop and use the random object like you do now with .NextDouble

 

Using the refresh might just slow down your code enough to get a different seed each time the Random is used as the system has to check the message queue for the form and handle everything in it. Also using the debugger really slows the execution down so the random generator gets a different seed value.

Nothing is as illusive as 'the last bug'.
Posted

This will give you a random number:

 

/// <summary>

/// Returns a random number (0<=x<max)

/// </summary>

private int CreateRandomNumber(int max)

{

// advance timer:

Thread.Sleep(1);

// create random number:

Random autoRand = new Random();

 

return autoRand.Next() % max;

}

Greetings,

 

Toni.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...