Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Dice Generator Probability
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: Tue Feb 09, 2010 3:11 pm   Post subject: Dice Generator Probability

This application looks to be coded properly, but every time I run it, 1-4 "random" dice values are never landed on. Is this a bug with random and how long it takes to start at a new seed, or what?

Java:

/***********************************

    Program:    Question 7.17
    Date:       February 9, 2010

***********************************/

import java.util.Random;

public class Q717
{
    public static void main (String[] args)
    {
        int die[] = new int [2];            // Two die to roll
        int total = 0;                      // Stores addition of dice
        int probability[] = new int [12];   // Number of possibilities
        int amount = 36000;
        Random dice;                        // Random dice generator

        for (int i = 0 ; i < probability.length ; i++)  // For each possibility
            probability [i] = 0;                        // Sets counter to 0

        for (int i = 0 ; i < amount ; i++)   // Counts 36,000 times
        {
            dice = new Random ();
            die [0] = 1 + dice.nextInt (6);  // New random instance
            die [1] = 1 + dice.nextInt (6);  // New random instance
            total = die [0] + die [1];       // Add the results
            probability [total - 1] += 1;    // Increases counter on probability
        }

        // Graphical output
        System.out.println ("Value\t|\tChances of Rolling Value ( /" + amount + " )");
        System.out.println ("---------------------------------------------------");
        for (int i = 1 ; i < probability.length ; i++)
            System.out.println ((i + 1) + "\t|\t" + probability [i - 1]);
    }
}
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Feb 09, 2010 4:02 pm   Post subject: RE:Dice Generator Probability

You're creating a new instance of Random every iteration through the loop, which probably has something to do with it. Either initialize and use a single Random object for all rolls, or, if you need a new one each time for some reason, use SecureRandom() which gives cryptographically secure "true" random numbers.
Prince Pwn




PostPosted: Tue Feb 09, 2010 4:47 pm   Post subject: RE:Dice Generator Probability

Thanks again, DemonWasp. Here's my final code:

Java:

/***********************************

    Program:    Question 7.17
    Date:       February 9, 2010

***********************************/

import java.util.Random;

public class Q717
{
    public static void main (String[] args)
    {
        int die[] = new int [2];            // Two die to roll
        int total = 0;                      // Stores addition of dice
        int probability[] = new int [12];   // Number of possibilities
        int amount = 36000;
        Random dice;                        // Random dice generator

        for (int i = 0 ; i < probability.length ; i++)  // For each possibility
            probability [i] = 0;                        // Sets counter to 0

        dice = new Random ();

        for (int i = 0 ; i < amount ; i++)   // Counts X times
        {
            die [0] = 1 + dice.nextInt (6);  // New random instance
            die [1] = 1 + dice.nextInt (6);  // New random instance
            total = die [0] + die [1];       // Add the results
            probability [total - 2] += 1;    // Increases counter on probability
        }

        // Graphical output
        System.out.println ("Value\t|\tChances of Rolling Value ( /" + amount + " )");
        System.out.println ("---------------------------------------------------");
        for (int i = 1 ; i < probability.length ; i++)
            System.out.println ((i + 1) + "\t|\t" + probability [i - 1]);
    }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: