android - How to Generate Single Random value without Duplicate? -


i doing quize app. in app questions wont generate duplicates. using code int value=random.nextint(10-1)+1.when submit answer random number generate newly generating duplicates.how can compare previous random value new random values every time ?

  1. generate 1 10 , store in list
  2. shuffle list of generated numbers
  3. keep removing list

    list<integer> list = new linkedlist<integer>();

    for (int = 1; <= 10; i++) {     list.add(i) }  collections.shuffle(list);  int value= list.remove(0); .......  value= list.remove(0); 

and on...

check : java - generate random range of specific numbers without duplication of numbers - how to?

also storing in hashmap , checking smart way other answer says. can cause lot more clashes, since everytime try add duplicate hashmap fail , have generate new 1 again. generating @ once , shuffling doesnt cause this. since input set small(10) collision might not happen much(depending on randomness, or maybe happens much?) , o(1) access map elements comparison help.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -