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 ?
- generate 1 10 , store in list
- shuffle list of generated numbers
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
Post a Comment