CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2010
    Posts
    6

    Help with getting new random numbers

    Back again. In my guessing game I have a random number generator and I want to create an option to replay the game but I don't know how to replay without the loop using the same random number. I originally put the generator in a do/while loop to encompass the main part of the method but it only used the same random number.

    Here's the code:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Random;



    public class Bagels {


    public static void main(String[] args) {
    BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
    System.out.println("Welcome to Bagels the Guessing Game!");
    System.out.println("Press [Enter] to continue");

    try {input.readLine();}
    catch (IOException e) {};

    String user1 = new String ();

    String rulesornot = new String ();
    System.out.println("To learn the rules, type (1) and press [Enter].");
    System.out.println(" ");
    System.out.println("To skip to the game, type (2) and press [Enter].");

    try {rulesornot = input.readLine();}
    catch (IOException e) {}
    int rules;
    rules = Integer.parseInt(rulesornot);

    int userin1;
    int userin2;
    int userin3;

    if (rules == 1) {
    System.out.println("Rules: You are to guess a 3-digit number and you may not repeat a digit within an answer.");
    System.out.println("If you guess correct on one number and it is in the correct spot that is \"Pico\".");
    System.out.println("If you guess correct on one number but it is not in the correct spot that is \"Fermi\".");
    System.out.println("If you don't guess any numbers correctly, than you will receive \"BAGELS\".");
    System.out.println("Press [Enter] to continue");

    try {input.readLine();}
    catch (IOException e) {};

    System.out.println("Begin guessing...");
    }
    else if((rules == 2) || (rules == 3))
    System.out.println("Begin guessing...");

    Random r = new Random();
    int num;
    int d1;
    int d2;
    int d3;
    int replay;
    String replayAgain = new String ();

    do{//generates a unique random 3 digit number
    num = r.nextInt(900) + 100;
    d1 = num % 10;
    d2 = ((num - d1) / 10) % 10;
    d3 = (num - d1 -(d2 * 10)) / 100;

    }while (d1 == d2|| d1 == d3 || d2 == d3);

    int guessNumber;

    do{

    do{

    try {user1 = input.readLine();}//Input for guessing
    catch (IOException e) {}
    guessNumber = Integer.parseInt(user1);

    userin1 = guessNumber % 10;//sets user inputed digits against random number's digits
    userin2 = ((guessNumber - userin1) / 10) % 10;
    userin3 = (guessNumber - userin1 -(userin2 * 10)) / 100;

    if (userin1 == userin2 || userin1 == userin3 || userin2 == userin3){
    System.out.println("All digits must be different.");
    }

    if (num == guessNumber){
    System.out.println("WINNER!");
    }

    if (d1 == userin1){
    System.out.println("Pico ");

    }

    if (d2 == userin2){
    System.out.println("Pico ");

    }

    if (d3 == userin3){
    System.out.println("Pico ");

    }

    if (d1 == userin2){
    System.out.println("Fermi ");

    }

    if (d1 == userin3){
    System.out.println("Fermi ");

    }

    if (d2 == userin1){
    System.out.println("Fermi ");

    }

    if (d2 == userin3){
    System.out.println("Fermi ");

    }

    if (d3 == userin1){
    System.out.println("Fermi ");

    }

    if (d3 == userin2){
    System.out.println("Fermi ");

    }
    if (d1 != userin1 && d1 != userin2 && d1 != userin3 && d2 != userin1 && d2 != userin2 && d2 != userin3 && d3 != userin1 && d3 != userin2 && d3 != userin3){
    System.out.println("BAGELS!");
    }


    }while (num != guessNumber);

    if (num == guessNumber){
    System.out.println("Want to play again?");
    System.out.println("Type (1) and press [Enter] to replay or type (2) and press [Enter] to leave.");
    }
    replayAgain = new String ();
    try {replayAgain = input.readLine();}
    catch (IOException e) {}
    replay = Integer.parseInt(replayAgain);

    if (replay == 1){
    System.out.println("You have to restart the game becuase I don't know how to.");
    }
    else {
    System.out.println("Go home, quitter!");

    }
    }while (replay == 1);


    }

    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Help with getting new random numbers

    The Random class will take a arg for the seed to generate the numbers. Use the system's time. Then every time you execute your program you will get a new series of numbers.

    BTW please use code tags around your code to preserve formatting. Info here: http://www.java-forums.org/misc.php?do=bbcode#code
    Norm

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Help with getting new random numbers

    Quote Originally Posted by IMcCool View Post
    Back again. In my guessing game I have a random number generator and I want to create an option to replay the game but I don't know how to replay without the loop using the same random number.
    You don't have to do anything.

    Each time a new Random() object is created a different seed will be used so you get a different random sequence.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Help with getting new random numbers

    Quote Originally Posted by Norm View Post
    The Random class will take a arg for the seed to generate the numbers. Use the system's time. Then every time you execute your program you will get a new series of numbers.
    If you don't specify a seed, a different seed will be used each time a Random object is created. So seeding from the system clock is not only superfluous, it can lead to problems.

    Before Java 1.5, Random actually did seed from the system clock. If many Random objects were created within a short timeframe, about 1 millisecond or so, they all ended up with the same seed thus producing exactly the same random sequence. This is because the system clock has a resolution of approximately 1 millisecond. If it's read many times during that interval it returns the same value.

    So never seed from the system clock. Instead rely on the Random default constructor to give you a unique random sequence. From the API:

    "Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured