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;}}}}
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
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.
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?
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.
Re: Random does not repeat itself?
Quote:
Originally Posted by
swedishfished
It still doesn't repeat any integers,
What doesn't repeat any integers? In other words, what code are you using now?
Quote:
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.
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..