我有一组固定的号码从1至5。这些被添加到地图如下:
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);
这是我随意拿,我从取出两个数地图<整数,整数GT;
留下三个数字
From that I randomly take two numbers that I remove from the Map<Integer, Integer>
leaving three numbers.
map.remove(Rwiner);
map.remove(Rsec);
这会离开我的三个数字像2,1,4。我想挑选出从一个随机数。我应该怎么办?
This would leave me three numbers like 2,1,4. I want to pick out a random number from that. What should i do?
这样的事情也许是:
// Setup
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);
map.put(5, 5);
int Rwiner = 3, Rsec = 5;
map.remove(Rwiner);
map.remove(Rsec);
// Get random entry from current map.
Random rnd = new Random();
int randomIndex = rnd.nextInt(map.size());
int randomKey = new ArrayList<Integer>(map.keySet()).get(randomIndex);
System.out.printf("Random entry: %d -> %d", randomKey, map.get(randomKey));