I have a group of fixed numbers from 1 to 5. These are added into a Map as follows:
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);
map.put(5, 5);
From that I randomly take two numbers that I remove from the Map<Integer, Integer>
leaving three numbers.
map.remove(Rwiner);
map.remove(Rsec);
This would leave me three numbers like 2,1,4. I want to pick out a random number from that. What should i do?
Are you asking how in Java to select three distinct random integers in the range 1..5?
How about creating a
List
with the values 1, 2, 3, 4, 5, then callingCollections.shuffle
then just looking at the first three elements?Source code:
Uh, from what little I understand, you need to keep removing a number randomly from your map. I think an array is better suited for your task.
However, each time you remove an object randomly, you will have to repeat these steps, and therefore, keep creating a new array each time (or as aioobe suggested, you will need to keep copying the set values into an arraylist) and this is wasteful. Lets see if we can do this more efficiently only using one array (and not creating a new one each time):
Example:
So the above method is more efficient as the array knows that the first k elements are dead. Implementation is as follows:
So the above method is way more efficient as it works on the same array, rather than creating a new array of arraylist each time. Plus, you can pick multiple numbers by changing the pickHowMany parameter- the method runs in O(n). Do take note that it works on arrays, so all you need to do is convert your map into an array once.
Ray's solution is good too, but it is inefficient if you shuffle each time you want to pick one (i.e. O(n^2)). However, if you shuffle only once, Ray's solution runs at O(n) and you can subsequently pick how many ever numbers you want from the list. So this is again at O(n) for multiple numbers, which is the same complexity as mine although, traversing through a list to remove the first n numbers is far clunkier than selecting the first n numbers of an array.
Something like this perhaps: