java - Generating random numbers in a particular range -


i trying generate n random numbers between 0-31 in android code. below code using:

int max_range = 31; securerandom securerandom = new securerandom(); int[] digestcodeindicesarr = new int[indices_length]; int = 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 number of random numbers need. it's 6,7 or 9. when print generated array, end seeing duplicates. can point out mistake making. have added below line of code filter out random duplicates:

if (!arrays.aslist(digestcodeindicesarr).contains(random_temp)) 

thanks in advance!

arrays.aslist(digestcodeindicesarr) not produce list<integer> size() == digestcodeindicesarr.length.
produces list<int[]> size() == 1, first (and only) element array.
such, never contain random_temp, ! contains() true.

it bad performance create list , perform sequential search check duplicates. use set instead, maintain in parallel array, or use linkedhashset first, convert array.

anyway, explains why code wasn't working. duplicate link tunaki had provided , duplicate link provided in comment, explains how trying do.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -