Generate 4 digit unique random number in Java using predefined library? -


this question has answer here:

i want generate 4 digit unique random number in java.

suppose run application 1000 times each time should unique random number.

i tried uuid long random code. want random code in 4 digits.

so far tried following code -

int num = -1;  arraylist<integer> arnumber = new arraylist<integer>(); for(int x = 0; x < 10; x++) {     arnumber.add(x); } collections.shuffle(arnumber);  string strnum = ""; for(int = 0; < 4; i++)     strnum = strnum + arnumber.get(new random().nextint(10));  num = integer.parseint(strnum); 

this code works fine, doubt repeat number after thousand.

is there built-in/predefined functionality/library/algorithm available in java?

in understaning if want generate 4 digit unique random number means numbers between 0 , 9999. in case this

arraylist<integer> arnumber = new arraylist<integer>(); (int x = 0; x < 10; x++) {      ... 

will produce 10 numbers in list. this

class randomgenerator {     list<integer> ints = new arraylist<>();     int = 0;      randomgenerator() {         (int = 0; < 10000; i++) {             ints.add(i);         }         collections.shuffle(ints);     }      int nextint() {         return ints.get(i++);     } } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -