Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
How can I start a function that will randomly generate integers for 3 dice when I roll them at the same time?
My aim is to get the 3 numbers and put them beside each other (larger to smaller), for example if I roll 3,1,4 I need to display 431. I started the code by not sure how to continue. I created 3 labels and 3 textboxes for the 3 dice and a "Roll" button that will display random results but I am stuck and don't know how to proceed!
Can you please give me a push?
Thanks

What I have tried:

Random rnd = new Random();
int dice1 = rnd.Next(1,6); // creates a number between 1 and 6 from dice 1
int dice2 = rnd.Next(1,6);   // creates a number between 1 and 6 from dice 2
int dice3 = rnd.Next(1,6);  // creates a number between 1 and 6 from dice 3
Posted
Updated 12-Oct-18 4:56am
Comments
MadMyche 12-Oct-18 10:52am    
What are the results of this? Does it work as intended?
[no name] 12-Oct-18 11:00am    
Maybe it's my "version", but you may find you need to use .Next(1,7) to get numbers between 1 and 6!

You can put your results in a List<int>, use LINQ to sort, and then copy each value to its respective text box.

Start by moving the Random instance out of the method, and putting it into a class level variable:
private Random rnd = new Random();
void MyMethod()
    {
    int dice1 = rnd.Next(1,6); // creates a number between 1 and 6 from dice 1
    int dice2 = rnd.Next(1,6);   // creates a number between 1 and 6 from dice 2
    int dice3 = rnd.Next(1,6);  // creates a number between 1 and 6 from dice 3
    ...
    }
The reason for that is that Random is initialized with the system clock, so you don't want to create them over and over, or you will get the same sequence if you call your method too quickly. Making it class level, means it is created once, so you don't get that repetition.

Then, look at the definition of the Next method, and you will see that the upper bound is exclusive - the value returned will never equal it - so you need to add one in order to get a fair roll on a six sided die:
private Random myDie = new Random();
public int RollStandardDie()
    {
    return myDie.Next(1, 6 + 1);
    }

Then call RollStandardDie three times to get your values, and sort them. The easiest way is like this:
private Random myDie = new Random();
public int RollStandardDie()
    {
    return myDie.Next(1, 6 + 1);
    }
private void MyButton_Click(object sender, EventArgs ew)
    {
    int[] dice = new int[3];
    for (int i = 0; i < dice.Length; i++)
        {
        dice[i] = RollStandardDie();
        }
    Array.Sort(dice);
Then just feed your values into your Labels or TextBoxes:
label1.Text = dice[0].ToString();
TextBox2.Text = dice[1].ToString();
 
Share this answer
 
Comments
[no name] 12-Oct-18 11:56am    
The "purist" / OCD in me wants to use "parallel" throws because the requirement is to "roll 3 dice".

(For the I Ching, you can reduce "3 coin" tosses to one of 4 numbers).
OriginalGriff 12-Oct-18 11:59am    
Your OCD is dangerous: Random.Next is decidedly not thread safe! :laugh:
https://blogs.msdn.microsoft.com/pfxteam/2009/02/19/getting-random-numbers-in-a-thread-safe-way/
[no name] 12-Oct-18 12:27pm    
It never occurred to me to use the "same" Random for parallel operations.

One creates multiple instances in different points in time (if not explicitly "seeding").


OriginalGriff 12-Oct-18 12:43pm    
And on a multiple core machine, that means they all probably start from the same seed ... or you have to set up an array of the buggers! :laugh:
[no name] 12-Oct-18 13:12pm    
They're seeded when created; you don't "create" them at the same time.

The parallel operation is the "rolling".

(And you can re-initialize, randomly, while waiting for the user to hit the button).

You create state machines; not just an "array of randoms".

Thanks a lot for helping... is there a way to do the last bit without using an array? I know it might be a long solution but I just want to compare and learn the difference.
Thanks again
 
Share this answer
 
Comments
MadMyche 12-Oct-18 11:07am    
Instead of assigning the values to an array; you could use a StringBuilder and append on each die roll
OriginalGriff 12-Oct-18 11:34am    
Yes, but it's easier to sort with an array - and more flexible in the future.
But you can do it with "normal" code, it's just longer and more hassle:
if (a > c)
  {
  temp = a;
  a = c;
  c = temp;
  }
if (a > b)
  {
  temp = a;
  a = b;
  b = temp;
  }
if (b > c)
  {
  temp = b;
  b = c;
  c = temp;
  }
It gets a lot more long winded for four and on up!
Richard Deeming 12-Oct-18 13:39pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900