473,770 Members | 4,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<intability Rolls = new List<int>();

for (int i = 0; i < numberOfDice; i++)

{

abilityRolls.Ad d(die.Next( 1,7));

}

abilityRolls.So rt();

abilityRolls.Re verse();

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 3286
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<intability Rolls = new List<int>();

for (int i = 0; i < numberOfDice; i++)

{

abilityRolls.Ad d(die.Next( 1,7));

}

abilityRolls.So rt();

abilityRolls.Re verse();

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(o bject sender, EventArgs e)

{

string rollResults2;

string listResults="";

rollPnl.Visible = false;

racePnl.Visible = true;

this.myAbilitie s =new CharacterAbilit ies();

myAbilities=thi s.myAbilities ;

myAbilities.Set CharacterAbilit y("Rolled", "str", Die.RollAbility (out
rollResults2, Convert.ToInt32 (createDiceComb oBox.Text)));

strRaceLbl.Text = myAbilities.Get CharacterAbilit y("Rolled",
"str").ToString ();

listResults = listResults + rollResults2 + "\n";

myAbilities.Set CharacterAbilit y("Rolled", "dex", Die.RollAbility (out
rollResults2, Convert.ToInt32 (createDiceComb oBox.Text)));

dexRaceLbl.Text = myAbilities.Get CharacterAbilit y("Rolled",
"dex").ToString ();

listResults = listResults + rollResults2 + "\n";

myAbilities.Set CharacterAbilit y("Rolled", "con", Die.RollAbility (out
rollResults2, Convert.ToInt32 (createDiceComb oBox.Text)));

conRaceLbl.Text = myAbilities.Get CharacterAbilit y("Rolled",
"con").ToString ();

listResults = listResults + rollResults2 + "\n";

myAbilities.Set CharacterAbilit y("Rolled", "int", Die.RollAbility (out
rollResults2, Convert.ToInt32 (createDiceComb oBox.Text)));

intRaceLbl.Text = myAbilities.Get CharacterAbilit y("Rolled",
"int").ToString ();

listResults = listResults + rollResults2 + "\n";

myAbilities.Set CharacterAbilit y("Rolled", "wis", Die.RollAbility (out
rollResults2, Convert.ToInt32 (createDiceComb oBox.Text)));

wisRaceLbl.Text = myAbilities.Get CharacterAbilit y("Rolled",
"wis").ToString ();

listResults = listResults + rollResults2 + "\n";

myAbilities.Set CharacterAbilit y("Rolled", "cha", Die.RollAbility (out
rollResults2, Convert.ToInt32 (createDiceComb oBox.Text)));

chaRaceLbl.Text = myAbilities.Get CharacterAbilit y("Rolled",
"cha").ToString ();

listResults = listResults + rollResults2 + "\n";
dieRollsRaceLbl .Text = listResults;

raceComboBox.En abled = true;

}


"PokerMan" <no****@pokerca t.co.ukwrote in message
news:Ol******** ******@TK2MSFTN GP05.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<intability Rolls = new List<int>();

for (int i = 0; i < numberOfDice; i++)

{

abilityRolls.Ad d(die.Next( 1,7));

}

abilityRolls.So rt();

abilityRolls.Re verse();

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<intability Rolls = new List<int>();

for (int i = 0; i < numberOfDice; i++)

{

abilityRolls.Ad d(die.Next( 1,7));
This also uses the single static die.
>
}

abilityRolls.So rt();

abilityRolls.Re verse();

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******@coldm ail.comwrote in message
news:ag******** *************** *********@4ax.c om...
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
numberOfDic e)//this will be called with more that 4 dice

{
string _rollResults="" ;

Random die = new Random();
Delete this line.
>>

List<intability Rolls = new List<int>();

for (int i = 0; i < numberOfDice; i++)

{

abilityRolls.Ad d(die.Next( 1,7));
This also uses the single static die.
>>
}

abilityRolls.So rt();

abilityRolls.Re verse();

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****@pokerca t.co.ukwrote in message
news:%2******** ********@TK2MSF TNGP06.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******@coldm ail.comwrote in message
news:ag******** *************** *********@4ax.c om...
>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
initialisati on 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<intability Rolls = new List<int>();

for (int i = 0; i < numberOfDice; i++)

{

abilityRolls.Ad d(die.Next( 1,7));
This also uses the single static die.
>>>
}

abilityRolls.So rt();

abilityRolls.Re verse();

for (int i = 0; i < numberOfDice; i++)

{

_rollResults = _rollResults +
abilityRoll s[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
541
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 sizes properly so I know there is a message in there. I also found a keyboard shortcut where <ctrl>+<insert> will copy the contents of the messagebox to the clipboard in plain text. I see my messagebox text in the clipboard contents but not in the...
2
30164
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.) and would like to make the dialog look and act like the standard MessageBox. Therefore, I would like to put in the message section the same type of icons found in the MessageBox dialog, such as MessageBoxIcon.Exclamation. In another post here I had...
11
1840
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 System.Threading; using System.Web.Services;
0
4092
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; namespace Utils {
1
5255
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 procedure should check if the record number (seat number) is valid(this works fine), then if the record number(seat number) is vaild but not been assigned to anyone the app should throw up a messagebox "seat X is
3
1157
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 to know if anybody does know is if it is truly random. The code i am using is Private Sub btnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click
4
1401
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 string value to a label? Im new to vb . I 'm still trying to shake the happits of basica or gwbasic trobo basic. You get the Idea. So can someone get me on the right path here. -- //Bart//
2
1034
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. private void GenerateTestFiles_Click(object sender, EventArgs e) { StreamWriter outStream = null; string message = null; for (int i = 0; i < (100); i++) {
14
8676
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
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.