My teacher is obsessed with magic 8 balls and she asked how many times it would take to roll every answer. I wrote a reeealy simple program and for some reason it wont work. I know there are other ways to write this using more complicated number checks but this way should be fastest since java's slow as crap. Anyways I know the conditional statement works even if it seems strange. What happens though is the same random number is picked 40 times in a row. Just so u know there are 20 sides to a magic 8ball. here's the source
import java.util.Random;
public class magic
{
public static void main(String args[])
{
int trials=50; //running the program multiple times to get a better average
int results[]; //stores how many tries each trial took
results=new int[trials];
int average=0; //will become the average number of tries for all trials
for(int a=0;a<trials;a++) //runs the loop trial number of times
{
int numgotten=0; //number of times an appropriate number was rolled
int tries=0; //number of times the 8 ball has been rolled
while(numgotten!=20) //rolls the 8ball until all 20 answers have been given
{
tries++; //records that the 8ball was rolled
//System.out.println("numleft: "+(20-numgotten) + " " + " tries: "+tries); //debug helper
if(roll(20-numgotten)) //requests the roll method to be called
{
numgotten++; //records that an unused answer was rolled
//System.out.println("true"); //debug helper
}
else
{
//System.out.println("false");//debug helper
}
System.out.println(); //spacer
results[a]=tries; //stores the number of tries that were needed to roll all 20 sides
}
}
for(int b=0;b<trials;b++) //these three lines calculate the overall average of times the 8 ball was rolled
average+=results[b];
average/=(trials);
for(int b=0;b<trials;b++)
{
System.out.println(results[b]); //displays all the results from the different trials
for(int d=0;d<100000000;d++){} //the lazy programmer's delay
}
System.out.println("average of "+trials+" trials is "+average);
}
public static boolean roll(int a)
{
Random rand = new Random(); //problem is here or in next line
int numpicked=rand.nextInt(20)+1; //range is 1-20
//odds of getting new number is (20-numgotten)/20*100%
//this sees if the number rolled is 1 the answers not yet rolled
//you can imagine this as putting all the unpicked answers at the bottom and every time the answer is rolled
//that answer is moved to the top and all the unused answers are moved down
// System.out.println("random number:" +numpicked); //debug look close at this onw
if(numpicked<=a)//need to see if random number is 1 we havent gotten
return true;
else
return false;
}
}
**edit: i added in some comments but i dpnt have a compiler with me so i dont know if this compiles. The problem is that for some reason the same random number is being sent over and over many times in a row. java can be at least up to 8 times slower than c/c++ depending on what ure doing. And what's wrong with my programming?? itll be easier to read if u
import java.util.Random;
public class magic
{
public static void main(String args[])
{
int trials=50; //running the program multiple times to get a better average
int results[]; //stores how many tries each trial took
results=new int[trials];
int average=0; //will become the average number of tries for all trials
for(int a=0;a<trials;a++) //runs the loop trial number of times
{
int numgotten=0; //number of times an appropriate number was rolled
int tries=0; //number of times the 8 ball has been rolled
while(numgotten!=20) //rolls the 8ball until all 20 answers have been given
{
tries++; //records that the 8ball was rolled
//System.out.println("numleft: "+(20-numgotten) + " " + " tries: "+tries); //debug helper
if(roll(20-numgotten)) //requests the roll method to be called
{
numgotten++; //records that an unused answer was rolled
//System.out.println("true"); //debug helper
}
else
{
//System.out.println("false");//debug helper
}
System.out.println(); //spacer
results[a]=tries; //stores the number of tries that were needed to roll all 20 sides
}
}
for(int b=0;b<trials;b++) //these three lines calculate the overall average of times the 8 ball was rolled
average+=results[b];
average/=(trials);
for(int b=0;b<trials;b++)
{
System.out.println(results[b]); //displays all the results from the different trials
for(int d=0;d<100000000;d++){} //the lazy programmer's delay
}
System.out.println("average of "+trials+" trials is "+average);
}
public static boolean roll(int a)
{
Random rand = new Random(); //problem is here or in next line
int numpicked=rand.nextInt(20)+1; //range is 1-20
//odds of getting new number is (20-numgotten)/20*100%
//this sees if the number rolled is 1 the answers not yet rolled
//you can imagine this as putting all the unpicked answers at the bottom and every time the answer is rolled
//that answer is moved to the top and all the unused answers are moved down
// System.out.println("random number:" +numpicked); //debug look close at this onw
if(numpicked<=a)//need to see if random number is 1 we havent gotten
return true;
else
return false;
}
}
**edit: i added in some comments but i dpnt have a compiler with me so i dont know if this compiles. The problem is that for some reason the same random number is being sent over and over many times in a row. java can be at least up to 8 times slower than c/c++ depending on what ure doing. And what's wrong with my programming?? itll be easier to read if u
Comment