EFileTahi-A Posted October 15, 2004 Posted October 15, 2004 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? Quote
samsmithnz Posted October 15, 2004 Posted October 15, 2004 can you post a bit more code... Quote Thanks Sam http://www.samsmith.co.nz
EFileTahi-A Posted October 15, 2004 Author Posted October 15, 2004 (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 February 28, 2007 by PlausiblyDamp Quote
Wile Posted October 15, 2004 Posted October 15, 2004 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. Quote Nothing is as illusive as 'the last bug'.
ToniMontana Posted October 18, 2004 Posted October 18, 2004 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; } Quote Greetings, Toni.
EFileTahi-A Posted October 18, 2004 Author Posted October 18, 2004 Ahhhhh!!! I knew it was timed related!!! Ok, am going to do things diferent... ... thank u all for the help! - THREAD RESOLVED - Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.