You have a syntax error. boxCount has not been instantiated and is not a known type. You need to create the boxCount array before you attempt to use it. See example below:
public void shoes(int[] pairs)
{
System.out.println("We will roll go through the boxes 100 times and store the input for the variables");
int boxCount[] = new int[100];
for(int i=0; i < boxCount.length; i++)
{
//("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*6));
boxCount[i] = (int) (Math.random()*6) + 1;
}
}
Answer from cklab on Stack OverflowYou have a syntax error. boxCount has not been instantiated and is not a known type. You need to create the boxCount array before you attempt to use it. See example below:
public void shoes(int[] pairs)
{
System.out.println("We will roll go through the boxes 100 times and store the input for the variables");
int boxCount[] = new int[100];
for(int i=0; i < boxCount.length; i++)
{
//("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*6));
boxCount[i] = (int) (Math.random()*6) + 1;
}
}
int boxCount[i] = (int) (Math.random()*6) + 1;
This line of code looks like you are trying to create an array or something.
You want to create the array boxCount BEFORE your for loop:
int boxCount[] = new int[100];
Then you can do this in your loop:
boxCount[i] = (int) (Math.random()*6) + 1;
Videos
Your formula generates numbers between min and min + max.
The one Google found generates numbers between min and max.
Google wins!
A better approach is:
int x = rand.nextInt(max - min + 1) + min;
Your formula generates numbers between min and min + max.
Random random = new Random(1234567);
int min = 5;
int max = 20;
while (true) {
int x = (int)(Math.random() * max) + min;
System.out.println(x);
if (x < min || x >= max) { break; }
}
Result:
10
16
13
21 // Oops!!
See it online here: ideone
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
You can use the Random generator to generate a random index and return the element at that index:
//initialization
Random generator = new Random();
int randomIndex = generator.nextInt(myArray.length);
return myArray[randomIndex];