473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Random and MessageBox

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<intabilityRolls = 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]; ;

}

}
Feb 13 '07 #1
5 3250
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" <mu****@NoEmail.localwrote in message
news:yO******************************@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<intabilityRolls = 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]; ;

}

}


Feb 13 '07 #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;

}


"PokerMan" <no****@pokercat.co.ukwrote in message
news:Ol**************@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" <mu****@NoEmail.localwrote in message
news:yO******************************@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<intabilityRolls = 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]; ;

}

}



Feb 13 '07 #3
On Mon, 12 Feb 2007 20:21:35 -0500, "Muffin" <mu****@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<intabilityRolls = 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

Feb 13 '07 #4
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" <ro******@coldmail.comwrote in message
news:ag********************************@4ax.com...
On Mon, 12 Feb 2007 20:21:35 -0500, "Muffin" <mu****@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<intabilityRolls = 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

Feb 13 '07 #5
Thanks, it works great now!!!

"PokerMan" <no****@pokercat.co.ukwrote in message
news:%2****************@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" <ro******@coldmail.comwrote in message
news:ag********************************@4ax.com...
>On Mon, 12 Feb 2007 20:21:35 -0500, "Muffin" <mu****@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<intabilityRolls = 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


Feb 13 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Rich Tasker | last post by:
I have a strange situation. A simple call to MessageBox.Show("XXX", "YYY") does not display the message in the messagebox with a visible font. Based on the content of the messagebox, the box...
2
by: Dennis C. Drumm | last post by:
This is a restatement of an earlier post that evidently wasn't clear. I am building a custom MessageBox dialog that has, among other things, a few new button options (yes to all, no to all, etc.)...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
0
by: alex_f_il | last post by:
The class centers MessageBox inside the parent rectangle. Enjoy! using System; using System.Windows.Forms; using System.Text; using System.Drawing; using System.Runtime.InteropServices; ...
1
by: Steven Smith | last post by:
Hi Guys How do I check whether or not a specific record number in an initialised random access file is empty or not ? What I need to do is when DisplayButton.Click event is triggered the...
3
by: Barry | last post by:
I am looking for opinions on using random number generation in vb.net, I am using randomize and then using the rnd function, I have done a test and it seems to be pretty much random, what I'd like...
4
by: Dennis W | last post by:
I want to display a differnt message evry time a button is clicked. so I set up a aray of ms(12) so that I can asign 12 differnt strings to a labelbut I get a error . First of all can I asign a...
2
by: Shani Aulakh | last post by:
The following code is generating files perfectly, But when i change the date on my laptop to get files with different modified times. It just overrides all the files with the recent date. ...
14
by: santoshkakani | last post by:
Hi friends i can any one help me merging 2 random tables into one and displaying in gridview for example table 1 Sid SName Dno and table 2 Dno Dname Daddress
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.