java - Modify duplicating random number generator to non-duplicating random number generator -


int size = 5;  int[] list = new int[size];  random rand = new random(); for(int = 0; < size; i++) {    list[i] = rand.nextint(100);             }  for(int element : list)    system.out.print(element + " "); 

i'm trying modify random number generator not duplicate random generated numbers. how can accomplish this? help.

there various ways it. simple variant on have:

int size = 5; int[] values = new int[100]; int[] list = new int[size]; for( int = 0; < 100; i++ ) values[i] = i; random rand = new random(); int ctlistsize = 0; int xlist = 0; while( true ){     int icandidatevalue = rand.nextint(100);     if( values[ icandidatevalue ] == 0 ) continue; // used     list[ xlist++ ] = icandidatevalue;     values[ icandidatevalue ] = 0;     if( xlist == size || xlist == 100 ) break; }  for(int element : list) system.out.print(element + " "); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -