ChimeraTx ChimeraTx - 1 month ago 7
C# Question

C# - Need help generating a random number and using the same number multiple times in other functions

I'm trying to make a psudo-Shut The Box program that will roll dice and user has the option to choose between covering the number corresponding to both the dice or the sum of the dice.

It seems like everything is working perfectly so far except for one thing. When I try to click the button for either of the user input choices, it is clear that it is still generating random numbers due to the wrong check boxes being checked.

I suspect that this is a result of how I am calling the functions. Is there a way to pass the random value from dice one and dice two into something that will stay the same if called in other functions while being able to be randomly generated again after pressing the roll dice button?

private int firstDiceRandom()
{
Random diceOne = new Random();
int oneDie = diceOne.Next(1, 7);
System.Threading.Thread.Sleep(100);
return oneDie;
}

// Set value of Random number to a value
private int firstDiceValue()
{
int value = firstDiceRandom();
return value;
}

// Random number for Dice Two
private int secondDiceRandom()
{
Random diceTwo = new Random();
int twoDie = diceTwo.Next(1, 7);
return twoDie;
}

// Set value of Random number to a value
private int secondDiceValue()
{
int value = secondDiceRandom();
return value;
}

// Set value of Total random numbers
private int totalDice()
{
int totalDice = firstDiceValue() + secondDiceValue();
return totalDice;
}

// Check if boxes 7-12 are checked
private bool bothDiceBool()
{
if (checkBox7.Checked && checkBox8.Checked && checkBox9.Checked && checkBox10.Checked && checkBox11.Checked && checkBox12.Checked)
{
return false;
}
return true;
}

// Roll Dice Button and Generation of dice pictures for values.
private void rollDiceButton_Click(object sender, EventArgs e)
{
int oneDie = firstDiceValue();
int twoDie = secondDiceValue();

switch (oneDie)
{
case 1:
diceOneImage.Image = new Bitmap(@"location");
break;
case 2:
diceOneImage.Image = new Bitmap(@"location");
break;
case 3:
diceOneImage.Image = new Bitmap(@"location");
break;
case 4:
diceOneImage.Image = new Bitmap(@"location");
break;
case 5:
diceOneImage.Image = new Bitmap(@"location");
break;
case 6:
diceOneImage.Image = new Bitmap(@"location");
break;
}

//If 7-12 are all checked, not supposed to run, sends a blank picture where the die would be.
if (bothDiceBool() == true)
{

switch (twoDie)
{
case 1:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 2:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 3:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 4:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 5:
diceTwoImage.Image = new Bitmap(@"location");
break;
case 6:
diceTwoImage.Image = new Bitmap(@"location");
break;
}

}
// Blank die picture
else
{
diceTwoImage.Image = new Bitmap(@"location");
}
}

// User choice button
private void button2_Click(object sender, EventArgs e)
{

int buttonOneTotal = totalDice();

// If Choice One selected when button pressed
if (radioButton1.Checked)
{
switch (buttonOneTotal)
{
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true; radioButton1.Checked = false;
break;
case 7:
checkBox7.Checked = true;
radioButton1.Checked = false;
break;
case 8:
checkBox8.Checked = true;
radioButton1.Checked = false;
break;
case 9:
checkBox9.Checked = true;
radioButton1.Checked = false;
break;
case 10:
checkBox10.Checked = true;
radioButton1.Checked = false;
break;
case 11:
checkBox11.Checked = true;
radioButton1.Checked = false;
break;
case 12:
checkBox12.Checked = true;
radioButton1.Checked = false;
break;
}
}
// If Choice two selected when button pressed
else if (radioButton2.Checked)
{
int buttonTwoOne = firstDiceValue();
int buttonTwoTwo = secondDiceValue();

// First Die
switch (buttonTwoOne)
{
case 1:
checkBox1.Checked = true;
radioButton2.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton2.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton2.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton2.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton2.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton2.Checked = false;
break;
}

// Second Die
switch (buttonTwoTwo)
{
case 1:
checkBox1.Checked = true;
radioButton1.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton1.Checked = false;
break;
}
}
}

Answer

One solution that can help your case is declaring a variable outside the scope of this program. In other words a public variable.

public int RandomValueOne;

Now what you want to do is store your random value inside this public variable.

    private int firstDiceValue()
{
    RandomValueOne = firstDiceRandom();
    return RandomValueOne;
}

Now you can use your variable directly without any fear of numbers re-generating. Call your function "FirstDiceValue" ONCE and keep using the global variable isntead

Comments