samedi 26 novembre 2016

Arrays and random numbers

generate 1,000,000 Math.random() integers from 0 to 37 and calculate the percentage that each number appears

package arrays;
import java.util.List;
import java.util.Random;

public class arraysintro {

public static void main(String[] args){

    int numValues = 37;
    int[] array = randomArray(numValues);
    printArray(array);
}

public static int[] randomArray(int n) {
    int[] a = new int[n];
    for (int i = 0; i<a.length; i++) {
        a[i] = randomInt(0, 1000000);
    }
    return a;
}

private static int randomInt(int i, int j) {
    Random random = new Random();
    int abc = random.nextInt(1000000);
    return  abc;
}

public static void printArray(int[] a) {
    for (int i = 0; i<a.length; i++) {
        System.out.println(a[i]);
    }
}

}

Hi so I did most of the code everything works. I can get the 37 randomized 1 to 1000000 numbers but, I can't get the percentage for them. I tried creating a variable in the Random Int that would create the percentage but, since I can't create a double how do I approach this?




Aucun commentaire:

Enregistrer un commentaire