Salman Javed
Salman Javed

Reputation: 37

Dice Simulator int c#

i m facing a problem in dice simulator in c#.The function RandomGenerator generates a pair of dice until the sum of these two becomes equal to the given number(from 2 to 12) in parameter.A count variable holds the number of times the pair of dice is rolled.The problem is that when i enter an even number it correctly returns the count.But when i enter an odd number it does nothing,not even gives an error,the dash goes on blinking and blinking.The code is given below.Can anyone help me??

 using System.Threading.Tasks;
 namespace ConsoleApplication1
 {
    class Program
    {


        static int RandomGenerator(int n)                                    
        {

            Random rand1 = new Random();                                              
            Random rand2 = new Random();                                               

            int sum = rand1.Next(1,7) + rand2.Next(1,7);                                  
            int count = 1;                                                              

            {
                sum = rand1.Next(1,7) + rand2.Next(1,7);                                
                count++;
            }

            return count;

        }


        static void Main(string[] args)
        {
            Console.WriteLine("Hello! this program  a pair of dice until total on dice is equal to your given number.\n\n");

            Console.WriteLine("Enter the number :");
            int num = int.Parse(Console.ReadLine());

            int rolls = RandomGenerator(num);
            Console.WriteLine("The number of rolls are:" + rolls);

        }
    }
}

Upvotes: 0

Views: 10188

Answers (3)

Yuan Shing Kong
Yuan Shing Kong

Reputation: 674

Suggested solution from me:

public static int RandomGenerator(int n)
{
    Random random = new Random();
    int sum = 0;
    int count = 0;

    do
    {
        sum = random.Next(1, 7) + random.Next(1, 7);
        count++;
    } while (sum != n);

    return count;
}

Victor Efimov is right about the Random instance and I faced a similar issue once with the random generator for generating color :)

I also suggest you to perform sanity check on the user's input to make sure the values entered are always between 2 and 12. This is to avoid being caught in do-while loop when the condition sum != n will never come true.

Upvotes: 1

Victor Efimov
Victor Efimov

Reputation: 394

The problem is that you're using two Random instances. By default they're initialized with Environment.TickCount seed, which has precision about 15 milliseconds. This means that it's pretty much guaranteed that your instances of class Random get identical seed and hence generate identical values on every call to Next. The sum of two identical numbers is always even.

A proper solution would be to use a single instance of Random for both dice.

Upvotes: 6

LuisF
LuisF

Reputation: 564

Aren't you missing a while or for-loop?

I think you should have something like the code below in your RandomGenerator method:

static int RandomGenerator(int n)                                    
    {

        Random rand1 = new Random();                                               

        int sum = rand1.Next(1,7) + rand1.Next(1,7);                                  
        int count = 1;                                                              

        //while the sum variable isn't equal to your provided number, roll the dices again 
        while(sum != n)
        {
            sum = rand1.Next(1,7) + rand1.Next(1,7);                                
            count++;
        }

        return count;

    }

Upvotes: 0

Related Questions