mardi 24 septembre 2019

Range of values to populate randomly in loop

I am trying to write a method that will generate random integers in sorted order, and the limit of the random numbers should be log of how many are entered. The code I have written works when I do 10 integers, I get 0 for all the outputs which is correct. However, if I enter 100 or 1000 I don't get the values 0,1 or 0,1, and 2. I don't know why it isn't computing the correct way, I just need it to only initializes the integers in the array to be log of the number, but it doesn't work for anything greater than 10. Any help would be greatly appreciated, I have posted the code below.

public static void randomSortedLimited(ArrayList<Integer> inL,
                                                    int inHowMany) {

    inL.clear();
    int limit = (int) (Math.log10(inHowMany));
    //System.out.println(limit); added to make sure limit is calculated properly
    inL.add(0);
    for (int i = 1; i < inHowMany - 1; i++) {
        inL.add(i, inL.get(i-1) + new java.util.Random().nextInt(limit));
     }
}



Aucun commentaire:

Enregistrer un commentaire