Random and MessageBox

M

Muffin

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.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]; ;

}

}
 
P

PokerMan

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?
 
M

Muffin

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;

}




PokerMan said:
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 said:
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.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]; ;

}

}

 
R

rossum

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.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
 
P

PokerMan

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 said:
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.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
 
M

Muffin

Thanks, it works great now!!!



PokerMan said:
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 said:
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.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

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top