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
You can use IntStream ints() or DoubleStream doubles() available as of java 8 in Random class. something like this will work, depends if you want double or ints etc.
Random random = new Random();
int[] array = random.ints(100000, 10,100000).toArray();
you can print the array and you'll get 100000 random integers.
You need to add logic to assign random values to double[] array using randomFill method.
Change
public static double[] list(){
anArray = new double[10];
return anArray;
}
To
public static double[] list() {
anArray = new double[10];
for(int i=0;i<anArray.length;i++)
{
anArray[i] = randomFill();
}
return anArray;
}
Then you can call methods, including list() and print() in main method to generate random double values and print the double[] array in console.
public static void main(String args[]) {
list();
print();
}
One result is as follows:
-2.89783865E8
1.605018025E9
-1.55668528E9
-1.589135498E9
-6.33159518E8
-1.038278095E9
-4.2632203E8
1.310182951E9
1.350639892E9
6.7543543E7
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];