Thursday, 15 September 2011

sorting - Java generating random unique numbers from an interval of numbers -



sorting - Java generating random unique numbers from an interval of numbers -

i want generate random unique int numbers interval app freezes out (infinite loop).

int[] ids = new int[200]; for(int = 0; < ids.length; i++){ int temp = getunique(ids); ids[i] = temp; } private int getunique(int[] ids){ while(true){ int irand = random(0, ids.length); if( unique( ids, irand ) ) homecoming irand; } } private boolean unique(int[] arr, int i){ for(int k : arr){ if(k == i) homecoming false; } homecoming true; } private int random(int min, int max){ homecoming min + (int)(math.random() * ((max - min) + 1)); }

i have array of integers between 0 - 200 sorted randomly. don't know why, application freezing out. why? problem?

consider using collections.shuffle(...) randomize list.

for example:

class="lang-java prettyprint-override">integer[] ids = getarrayofnumbers(); list<integer> idlist = arrays.aslist(ids); collections.shuffle(idlist); ids = idlist.toarray(ids);

java sorting random unique

No comments:

Post a Comment