Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Reeling

Thread: Random does not repeat itself?

  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Random does not repeat itself?

    I made a three die yahtzee program REAL quick, so it may not be perfect. However, I noticed that I never actually got a yahtzee..


    import java.util.Random;
    class apples {
    public static void main(String[] args){
    Random dice= new Random();
    int number[]= new int[4];
    int counter=1;
    int finalcount=3;
    while(counter<2){
    int counter2=0;
    while(counter2<3){
    number[counter2] = 1+dice.nextInt(6);
    counter2++;
    System.out.println(number[counter2]);
    }
    int total=number[1]+number[2]+number[3];
    if(total==number[1]*3 && total==number[2]*3){
    System.out.println("yahtzee took"+finalcount);
    counter++;
    }
    finalcount++;
    if(finalcount==1000000){System.out.println("b");br eak;}}}}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Random does not repeat itself?

    To make your code readable: (1) indent blocks, (2) follow standard Java naming conventions (classes begin with a capital letter, and descriptive variable names would be a good idea rather than counter, counter2) and (3) use the "code" tags so that formatting is preserved when the code appears here. Put [code] at the start of the code and [/code] at the end.

    Your code ends up like

    Java Code:
    import java.util.Random;
    class Apples {
        public static void main(String[] args){ 
            Random dice= new Random();
            int number[]= new int[4];
            int counter=1;
            int finalcount=3;
            while(counter<2){
                int counter2=0;
                while(counter2<3){
                    number[counter2] = 1+dice.nextInt(6);
                    counter2++;
                    //System.out.println(number[counter2]);
                }
                int total=number[1]+number[2]+number[3];
                if(total==number[1]*3 && total==number[2]*3){
                    System.out.println("yahtzee took"+finalcount);
                    counter++;
                }
                finalcount++;
                if(finalcount==1000000){
                    System.out.println("b");
                    break;
                }
            }
        }
    }
    Remember that array indices start at zero. Your inner while loop sets the values of number[0], number[1] and number[2]. But the if condition that follows it refers to number[3]. number[3] is zero throughout the program and the condition in the if statement will never be true.

  3. #3
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Re: Random does not repeat itself?

    It still doesn't repeat any integers, also is there any easier way to check if all of my array ints are the same?

  4. #4
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Random does not repeat itself?

    So, if you were to create 7 dies and printed what number they rolled, would the computer explode? Perhaps you should test that out.
    DarrylBurke likes this.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Random does not repeat itself?

    Quote Originally Posted by swedishfished View Post
    It still doesn't repeat any integers,
    What doesn't repeat any integers? In other words, what code are you using now?

    also is there any easier way to check if all of my array ints are the same?
    Yes: first==second and second==third. For seven dice you could use a for loop and test each==next

    ---

    Also I agree with the others about printing out all of the dice - that way you can see what is going on.

  6. #6
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Re: Random does not repeat itself?

    Ok, so when my dice roll-- lets say we get 5,2,1, Then I never get three rolls of the same, even if I roll it 1000000 times.

    EDIT:: NVM I fixed it. However, on my last roll, i always get 0 for some reason..
    Last edited by swedishfished; 10-20-2012 at 06:04 PM.

Similar Threads

  1. How to repeat a command
    By 511prs in forum New To Java
    Replies: 4
    Last Post: 09-14-2012, 12:31 PM
  2. nextInt() repeat
    By trishtren in forum New To Java
    Replies: 3
    Last Post: 10-09-2011, 06:20 PM
  3. how to repeat a task again after it has been done
    By eyal3400 in forum New To Java
    Replies: 2
    Last Post: 07-19-2011, 11:03 PM
  4. how to repeat my method?
    By poupas in forum New To Java
    Replies: 6
    Last Post: 11-20-2010, 06:29 PM
  5. Help with a repeat
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 01-01-2010, 09:42 PM

Posting Permissions

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