c#:
I hope somebody can show me why I need to have a messagebox to get "random" numbers in my example. If I comment out the message box call that is in RollAbility() the numbers generated are not random , otherwise it works fine. I really would like to get rid of the messagebox call. thx static class Die { public static int Roll(int numberOfSides) { Random die= new Random(); return die.Next(1, numberOfSides+1); } public static int RollAbility(out string rollResults, int numberOfDice)//this will be called with more that 4 dice { string _rollResults=""; Random die = new Random(); List<int> abilityRolls = new List<int>(); for (int i = 0; i < numberOfDice; i++) { abilityRolls.Add(die.Next( 1,7)); } abilityRolls.Sort(); abilityRolls.Reverse(); for (int i = 0; i < numberOfDice; i++) { _rollResults = _rollResults + abilityRolls[i].ToString() + ","; } rollResults = _rollResults; MessageBox.Show(rollResults, "Dice Roles");//if this is commented out the numbers are not random return abilityRolls[0] + abilityRolls[1] + abilityRolls[2]; ; } }
I guess I must be doing something wrong from the dialog. My results are definetly not randon when I comment out the messagebox. I can even move the messagebox before the sorts and still get the same effect. It is call from a dialog that is called from my main frame. Here is the event from the dialog that calls this. I'm a little new at C# , so please keep the laughter to a roar. private void rollBtn_Click(object sender, EventArgs e) { string rollResults2; string listResults=""; rollPnl.Visible = false; racePnl.Visible = true; this.myAbilities =new CharacterAbilities(); myAbilities=this.myAbilities ; myAbilities.SetCharacterAbility("Rolled", "str", Die.RollAbility(out rollResults2, Convert.ToInt32(createDiceComboBox.Text))); strRaceLbl.Text = myAbilities.GetCharacterAbility("Rolled", "str").ToString(); listResults = listResults + rollResults2 + "\n"; myAbilities.SetCharacterAbility("Rolled", "dex", Die.RollAbility(out rollResults2, Convert.ToInt32(createDiceComboBox.Text))); dexRaceLbl.Text = myAbilities.GetCharacterAbility("Rolled", "dex").ToString(); listResults = listResults + rollResults2 + "\n"; myAbilities.SetCharacterAbility("Rolled", "con", Die.RollAbility(out rollResults2, Convert.ToInt32(createDiceComboBox.Text))); conRaceLbl.Text = myAbilities.GetCharacterAbility("Rolled", "con").ToString(); listResults = listResults + rollResults2 + "\n"; myAbilities.SetCharacterAbility("Rolled", "int", Die.RollAbility(out rollResults2, Convert.ToInt32(createDiceComboBox.Text))); intRaceLbl.Text = myAbilities.GetCharacterAbility("Rolled", "int").ToString(); listResults = listResults + rollResults2 + "\n"; myAbilities.SetCharacterAbility("Rolled", "wis", Die.RollAbility(out rollResults2, Convert.ToInt32(createDiceComboBox.Text))); wisRaceLbl.Text = myAbilities.GetCharacterAbility("Rolled", "wis").ToString(); listResults = listResults + rollResults2 + "\n"; myAbilities.SetCharacterAbility("Rolled", "cha", Die.RollAbility(out rollResults2, Convert.ToInt32(createDiceComboBox.Text))); chaRaceLbl.Text = myAbilities.GetCharacterAbility("Rolled", "cha").ToString(); listResults = listResults + rollResults2 + "\n"; dieRollsRaceLbl.Text = listResults; raceComboBox.Enabled = true; } [quoted text, click to view] "PokerMan" <nospam@pokercat.co.uk> wrote in message news:OlfEtVyTHHA.1552@TK2MSFTNGP05.phx.gbl... >I copy and pasted your code. And numbers came out random? > > i got these results: > > 6,4,3,2, > 4,3,2,2, > 5,5,1,1, > 6,5,5,4, > 5,5,3,2, > 6,6,5,1, > 6,4,3,1, > 6,5,3,2, > 4,4,1,1, > 5,5,5,4, > 5,1,1,1, > 6,5,4,1, > 6,6,5,4, > 5,4,1,1, > 6,5,4,3, > 5,4,1,1, > 4,4,3,3, > 3,2,2,1, > 4,3,3,3, > 4,3,2,1, > 5,4,3,2, > 4,3,2,1, > 5,5,4,2, > 6,4,3,3, > 5,5,1,1, > 6,3,3,1, > 5,5,2,1, > > Not sure its very random but it doesnt produce an obvious pattern. That is > without message box in. Maybe its the way you are calling it, i do it > like this: > > string strRollRes; > > Die.RollAbility(out strRollRes, 4); > > How are you doing it? > > > "Muffin" <muffin@NoEmail.local> wrote in message > news:yOednQW9WeDyj0zYnZ2dnUVZ_tqnnZ2d@comcast.com... >>I hope somebody can show me why I need to have a messagebox to get >>"random" numbers in my example. If I comment out the message box call that >>is in RollAbility() the numbers generated are not random , otherwise it >>works fine. I really would like to get rid of the messagebox call. >> >> thx >> >> static class Die >> { >> >> >> public static int Roll(int numberOfSides) >> >> { >> >> >> Random die= new Random(); >> >> return die.Next(1, numberOfSides+1); >> >> } >> >> public static int RollAbility(out string rollResults, int >> numberOfDice)//this will be called with more that 4 dice >> >> { >> >> >> string _rollResults=""; >> >> Random die = new Random(); >> >> >> List<int> abilityRolls = new List<int>(); >> >> for (int i = 0; i < numberOfDice; i++) >> >> { >> >> abilityRolls.Add(die.Next( 1,7)); >> >> } >> >> abilityRolls.Sort(); >> >> abilityRolls.Reverse(); >> >> for (int i = 0; i < numberOfDice; i++) >> >> { >> >> _rollResults = _rollResults + >> abilityRolls[i].ToString() + ","; >> >> } >> >> rollResults = _rollResults; >> >> MessageBox.Show(rollResults, "Dice Roles");//if this is commented >> out the numbers are not random >> >> return abilityRolls[0] + abilityRolls[1] + abilityRolls[2]; ; >> >> } >> >> } >> >> > >
I copy and pasted your code. And numbers came out random? i got these results: 6,4,3,2, 4,3,2,2, 5,5,1,1, 6,5,5,4, 5,5,3,2, 6,6,5,1, 6,4,3,1, 6,5,3,2, 4,4,1,1, 5,5,5,4, 5,1,1,1, 6,5,4,1, 6,6,5,4, 5,4,1,1, 6,5,4,3, 5,4,1,1, 4,4,3,3, 3,2,2,1, 4,3,3,3, 4,3,2,1, 5,4,3,2, 4,3,2,1, 5,5,4,2, 6,4,3,3, 5,5,1,1, 6,3,3,1, 5,5,2,1, Not sure its very random but it doesnt produce an obvious pattern. That is without message box in. Maybe its the way you are calling it, i do it like this: string strRollRes; Die.RollAbility(out strRollRes, 4); How are you doing it? [quoted text, click to view] "Muffin" <muffin@NoEmail.local> wrote in message news:yOednQW9WeDyj0zYnZ2dnUVZ_tqnnZ2d@comcast.com... >I hope somebody can show me why I need to have a messagebox to get "random" >numbers in my example. If I comment out the message box call that is in >RollAbility() the numbers generated are not random , otherwise it works >fine. I really would like to get rid of the messagebox call. > > thx > > static class Die > { > > > public static int Roll(int numberOfSides) > > { > > > Random die= new Random(); > > return die.Next(1, numberOfSides+1); > > } > > public static int RollAbility(out string rollResults, int > numberOfDice)//this will be called with more that 4 dice > > { > > > string _rollResults=""; > > Random die = new Random(); > > > List<int> abilityRolls = new List<int>(); > > for (int i = 0; i < numberOfDice; i++) > > { > > abilityRolls.Add(die.Next( 1,7)); > > } > > abilityRolls.Sort(); > > abilityRolls.Reverse(); > > for (int i = 0; i < numberOfDice; i++) > > { > > _rollResults = _rollResults + > abilityRolls[i].ToString() + ","; > > } > > rollResults = _rollResults; > > MessageBox.Show(rollResults, "Dice Roles");//if this is commented > out the numbers are not random > > return abilityRolls[0] + abilityRolls[1] + abilityRolls[2]; ; > > } > > } > >
On Mon, 12 Feb 2007 20:21:35 -0500, "Muffin" <muffin@NoEmail.local> [quoted text, click to view] wrote: >I hope somebody can show me why I need to have a messagebox to get "random" >numbers in my example. If I comment out the message box call that is in >RollAbility() the numbers generated are not random , otherwise it works >fine. I really would like to get rid of the messagebox call. > >thx >
You are sreating too many new Random objects. Each new Random object is initialised fronm the system time. If they are created too close together then they will be initialised with the same time and will produce the same stream of pseudorandom numbers. Your MessageBox is probably causing enough delay in the loop so that the Random objects are initialised with different times. Normally you should only create one static Random object and use it as required in your class. With only one Random object and one initialisation there is no danger of different Random objects having the same initialisation. [quoted text, click to view] >static class Die >{ >
private static Random die = new Random(); This is the single static Random object. [quoted text, click to view] > > public static int Roll(int numberOfSides) > > { > > > Random die= new Random(); Delete this line. > > return die.Next(1, numberOfSides+1); This now uses the single static die. > > } > > public static int RollAbility(out string rollResults, int >numberOfDice)//this will be called with more that 4 dice > > { > > > string _rollResults=""; > > Random die = new Random(); Delete this line. > > > List<int> abilityRolls = new List<int>(); > > for (int i = 0; i < numberOfDice; i++) > > { > > abilityRolls.Add(die.Next( 1,7)); This also uses the single static die. > > } > > abilityRolls.Sort(); > > abilityRolls.Reverse(); > > for (int i = 0; i < numberOfDice; i++) > > { > > _rollResults = _rollResults + abilityRolls[i].ToString() >+ ","; > > } > > rollResults = _rollResults; > > MessageBox.Show(rollResults, "Dice Roles");//if this is commented >out the numbers are not random
The numbers should now be random even with this line commented out. [quoted text, click to view] > > return abilityRolls[0] + abilityRolls[1] + abilityRolls[2]; ;
Minor point, you have an extra semicolon here. [quoted text, click to view] > > } > >}
rossum
I agree with rossum. I think this is your issue too. I did your code via generating a new random on a mouse click, so there was a delay. [quoted text, click to view] "rossum" <rossum48@coldmail.com> wrote in message news:agg3t2pkl733ui3o4a2fdt6fn35ookkeun@4ax.com... > On Mon, 12 Feb 2007 20:21:35 -0500, "Muffin" <muffin@NoEmail.local> > wrote: > >>I hope somebody can show me why I need to have a messagebox to get >>"random" >>numbers in my example. If I comment out the message box call that is in >>RollAbility() the numbers generated are not random , otherwise it works >>fine. I really would like to get rid of the messagebox call. >> >>thx >> > You are sreating too many new Random objects. Each new Random object > is initialised fronm the system time. If they are created too close > together then they will be initialised with the same time and will > produce the same stream of pseudorandom numbers. Your MessageBox is > probably causing enough delay in the loop so that the Random objects > are initialised with different times. > > Normally you should only create one static Random object and use it as > required in your class. With only one Random object and one > initialisation there is no danger of different Random objects having > the same initialisation. > > >>static class Die >>{ >> > private static Random die = new Random(); > > This is the single static Random object. > >> >> public static int Roll(int numberOfSides) >> >> { >> >> >> Random die= new Random(); > Delete this line. >> >> return die.Next(1, numberOfSides+1); > This now uses the single static die. >> >> } >> >> public static int RollAbility(out string rollResults, int >>numberOfDice)//this will be called with more that 4 dice >> >> { >> >> >> string _rollResults=""; >> >> Random die = new Random(); > Delete this line. >> >> >> List<int> abilityRolls = new List<int>(); >> >> for (int i = 0; i < numberOfDice; i++) >> >> { >> >> abilityRolls.Add(die.Next( 1,7)); > This also uses the single static die. >> >> } >> >> abilityRolls.Sort(); >> >> abilityRolls.Reverse(); >> >> for (int i = 0; i < numberOfDice; i++) >> >> { >> >> _rollResults = _rollResults + >> abilityRolls[i].ToString() >>+ ","; >> >> } >> >> rollResults = _rollResults; >> >> MessageBox.Show(rollResults, "Dice Roles");//if this is commented >>out the numbers are not random > The numbers should now be random even with this line commented out. > >> >> return abilityRolls[0] + abilityRolls[1] + abilityRolls[2]; ; > Minor point, you have an extra semicolon here. > >> >> } >> >>} > > > rossum >
Thanks, it works great now!!! [quoted text, click to view] "PokerMan" <nospam@pokercat.co.uk> wrote in message news:%23bWaLs3THHA.5108@TK2MSFTNGP06.phx.gbl... >I agree with rossum. I think this is your issue too. I did your code via >generating a new random on a mouse click, so there was a delay. > > > > > "rossum" <rossum48@coldmail.com> wrote in message > news:agg3t2pkl733ui3o4a2fdt6fn35ookkeun@4ax.com... >> On Mon, 12 Feb 2007 20:21:35 -0500, "Muffin" <muffin@NoEmail.local> >> wrote: >> >>>I hope somebody can show me why I need to have a messagebox to get >>>"random" >>>numbers in my example. If I comment out the message box call that is in >>>RollAbility() the numbers generated are not random , otherwise it works >>>fine. I really would like to get rid of the messagebox call. >>> >>>thx >>> >> You are sreating too many new Random objects. Each new Random object >> is initialised fronm the system time. If they are created too close >> together then they will be initialised with the same time and will >> produce the same stream of pseudorandom numbers. Your MessageBox is >> probably causing enough delay in the loop so that the Random objects >> are initialised with different times. >> >> Normally you should only create one static Random object and use it as >> required in your class. With only one Random object and one >> initialisation there is no danger of different Random objects having >> the same initialisation. >> >> >>>static class Die >>>{ >>> >> private static Random die = new Random(); >> >> This is the single static Random object. >> >>> >>> public static int Roll(int numberOfSides) >>> >>> { >>> >>> >>> Random die= new Random(); >> Delete this line. >>> >>> return die.Next(1, numberOfSides+1); >> This now uses the single static die. >>> >>> } >>> >>> public static int RollAbility(out string rollResults, int >>>numberOfDice)//this will be called with more that 4 dice >>> >>> { >>> >>> >>> string _rollResults=""; >>> >>> Random die = new Random(); >> Delete this line. >>> >>> >>> List<int> abilityRolls = new List<int>(); >>> >>> for (int i = 0; i < numberOfDice; i++) >>> >>> { >>> >>> abilityRolls.Add(die.Next( 1,7)); >> This also uses the single static die. >>> >>> } >>> >>> abilityRolls.Sort(); >>> >>> abilityRolls.Reverse(); >>> >>> for (int i = 0; i < numberOfDice; i++) >>> >>> { >>> >>> _rollResults = _rollResults + >>> abilityRolls[i].ToString() >>>+ ","; >>> >>> } >>> >>> rollResults = _rollResults; >>> >>> MessageBox.Show(rollResults, "Dice Roles");//if this is commented >>>out the numbers are not random >> The numbers should now be random even with this line commented out. >> >>> >>> return abilityRolls[0] + abilityRolls[1] + abilityRolls[2]; ; >> Minor point, you have an extra semicolon here. >> >>> >>> } >>> >>>} >> >> >> rossum >> > >
Don't see what you're looking for? Try a search.
|