I am trying to generate n
random numbers between 0-31 in my Android code. Below is the code that I am using:
int max_range = 31;
SecureRandom secureRandom = new SecureRandom();
int[] digestCodeIndicesArr = new int[indices_length];
int i = 0, random_temp = 0;
while (i != indices_length-1) {
random_temp = secureRandom.nextInt(max_range);
if (!Arrays.asList(digestCodeIndicesArr).contains(random_temp)) {
digestCodeIndicesArr[i] = random_temp;
i++;
}
}
indices_length
is the number of random numbers that I need. It's generally 6,7 or 9. But when I print the generated array, I generally end up seeing duplicates. Can someone point out the mistake I am making. I have added the below line of code to filter out random duplicates:
if (!Arrays.asList(digestCodeIndicesArr).contains(random_temp))
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire