Jump to content

How to make the dice produce two different numbers when rolled.

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Tyga

Tyga

    Newbie

  • Members
  • Pip
  • 3 posts
I'm super new to programming and going by the little I know(which is very little!), I came up with this code:


static class program
{
const int SIX = 6;
const int ONE = 1;
static void Main()
{


char anS;

//Using if/else statements to allow for multiple conditions.
do
{
////Prompt user to roll dice
Console.WriteLine("Would you like to roll dice?");
anS = char.Parse(Console.ReadLine());
anS = char.ToUpper(anS);
//Convert characters entered into uppercase

//User will be prompted try again incase their response is not 'Y' or 'N'
while (anS != 'Y' && anS != 'N')

{
Console.WriteLine("You entered an invalid response,try again");
anS = char.Parse(Console.ReadLine());
anS = char.ToUpper(anS);
//The new input is tested again and until the user enters the correct
//character, they'll be asked to re-enter.
}



//Find out whether input is Y and execute the dice code
if (anS == 'Y')
{
//The code to execute whenever Y is a response.
int nuM1 = 0, nuM2 = 0;
Random randomNum1 = new Random();
nuM1 = randomNum1.Next(SIX);
// I figured if I have two rand num generators, I'd get different random numbers, its not the case
Random randomNum = new Random();
nuM2 = randomNum.Next(SIX);
// int nuM1 = 0, nuM2 = 0;

// for (int i = 0; i < SIX; i++) ;
{

// nuM2 = randomNum.Next(SIX);

if ((nuM1 == SIX) || (nuM2 == SIX))
{
//nested statements to print out different results when rolling dice.
Console.WriteLine("You rolled a box of cars");
Console.ReadLine();
}

else if ((nuM1 == ONE) || (nuM2 == ONE))
{
Console.WriteLine("You rolled snake ears");
Console.ReadLine();
}
else
{
Console.WriteLine("You rolled {0} and {0}", nuM1, nuM2);
Console.ReadLine();
}
}
}

}
while (anS == 'Y');
//The dice code will be executed as long as the answer is yes.
//The other option is 'N' which will print out the below message.
Console.WriteLine("Goodbye!");
Console.ReadLine();

Console.ReadLine()
The program compiles but the dice are always the same numbers!! ie you rolled 3 and 3 or 4 and 4...and so on. Please help :crying:

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
The Random class is generating the same numbers because they are using the same seed. Change the seed for the second one and it will be fine.

#3
Tyga

Tyga

    Newbie

  • Members
  • Pip
  • 3 posts
Ok, I changed the seed for the second one and now it has different numbers which is cool, thanks. But they dont appear as random as they ought to be. For instance I roll the same set of numbers the first 4 rolls and a particular set , 1 and 1 are being rolled 7 out of 10 times..is that normal?

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
There are 2 bugs in your code:

#1: your if statements are checking for "OR" instead of "AND" conditions.
#2: your WriteLine only accesses the first parameter {0}, so it's ALWAYS going to display doubles, even when they are not doubles.

#5
Tyga

Tyga

    Newbie

  • Members
  • Pip
  • 3 posts
Awesome. Changed the if statements to check for 'AND' and the randomness is splendid, thanks!
The placeholders as well.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users