Beginnerquestion: How to count amount of each number drawn in a Lottery and output it in a list?
Posted
by elementz
on Stack Overflow
See other posts from Stack Overflow
or by elementz
Published on 2010-06-01T16:14:15Z
Indexed on
2010/06/01
17:03 UTC
Read the original article
Hit count: 154
I am writing this little Lottery application. Now the plan is, to count how often each number has been drawn during each iteration of the Lottery, and store this somewhere. My guess is that I would need to use a HashMap, that has 6 keys and increments the value by one everytime the respective keys number is drawn. But how would I accomplish this? My code so far:
public void numberCreator()
{
// creating and initializing a Random generator
Random rand = new Random();
// A HashMap to store the numbers picked.
HashMap hashMap = new HashMap();
// A TreeMap to sort the numbers picked.
TreeMap treeMap = new TreeMap();
// creating an ArrayList which will store the pool of availbale Numbers
List<Integer>numPool = new ArrayList<Integer>();
for (int i=1; i<50; i++){
// add the available Numbers to the pool
numPool.add(i);
hashMap.put(nums[i], 0);
}
// array to store the lotto numbers
int [] nums = new int [6];
for (int i =0; i < nums.length; i++){
int numPoolIndex = rand.nextInt(numPool.size());
nums[i] = numPool.get(numPoolIndex);
// check how often a number has been called and store the new amount in the Map
int counter = hashMap.get
numPool.remove(numPoolIndex);
}
System.out.println(Arrays.toString(nums));
}
Maybe someone can tell me if I have the right idea, or even how I would implement the map properly?
© Stack Overflow or respective owner