Lotto counting Numbers& percentage Ideas/ Help

Hi guys,

Just curious on what ppl thought on methodolgy ...what ppl would suggest could be better.Thanks.

Anyway,I've just made a lottery program for fun which randomly 6 non duplicate numbers from a range of 0 - 49.These numbers get saved to a text file. I want to spice it up by it outputting the top numbers generated with a percentage showing written to next to each number, e.g. Out of 10000 numbers the 6 most pop numbers produced are: 6 = 15 %, 32 = 12%, 3 = 10%, 4 = 7% etc...

--

To achieve the first part above my intuitive way would be to firstly creating a 'while there is next element/string loop'

Each number is seperated by a space, so I would do a check for each number to see if the 2nd value is null, if it is then count the first string/int as the number. (or maybe next element method may recognise the space).

I will then make an int array storage with a space of 0-49.

Each int checked will create an incrementation (+1) to an array location corresponding to the value of int.It will also increment int count, which will be refered to later when finding the percentage of a number.

So basically the process is:

while(textfile.hasnextelement)

int value = Integer.parseInt(string);

array[value] = ++;

count ++;

Once the above has finished I should get an array storage index of 0-49 with so many values in each one with index number representing the individual value. Next I will do:

for(int i= 0; i < count; i++)

{

float percent = (count / index)*100;

//cut it down to 2 decimal points

index = percent;

}

Once the array is refilled with percentages. Each value is taken and stored in a int 'name' e.g, int one = index[1], etc to 49. After this the array can be sorted.

Then index[1] with a diffrent value can be referenced and compared to the value of the number name list e.g, index[1] has a value of 35 (percentage) which is int thirty. The top 6 comparisons can be printed with a referance to its number name (converted to string) with int percentage value.

Hope enjoyed reading. Open to ideas to improve/ tackle this before i still implementing.

Thanks

Message was edited by:

ra2or

[2291 byte] By [ra2ora] at [2007-10-2 23:47:48]
# 1
Hi,I would read in the numbers, and store them in a HashMap. The number would be the key, and the value would be a class which holds a counter and the number (so the number is duplicated, it exists as both key and part of the value).Kaj
kajbja at 2007-7-14 16:32:38 > top of Java-index,Java Essentials,Java Programming...
# 2
Why exactly would you hash a number that is known to be an integer between 1 and 49 in order to generate an index for a counter. Other than slowing things down, taking more space and requiring that you link in an extra class, what benefit do you get?
marlin314a at 2007-7-14 16:32:38 > top of Java-index,Java Essentials,Java Programming...
# 3

> Why exactly would you hash a number that is known to

> be an integer between 1 and 49 in order to generate

> an index for a counter. Other than slowing things

> down, taking more space and requiring that you link

> in an extra class, what benefit do you get?

Because the requirement could later be changed so that you are generating 49 random numbers in the range 0 - 1.000.000 What do you do then?

The HashMap can also easilly be changed to a TreeMap which can be sorted using different comparators. It's also very easy to implement the Visitor pattern and have many different visitors which can work with the data in the value class.

Most of those things can of course be done with the array as well, but my gut feeling says that the array would become a problem in the future (most of the other code is generic and could be applied to other games)

Kaj

kajbja at 2007-7-14 16:32:38 > top of Java-index,Java Essentials,Java Programming...