As each number is generated, check the current numbers array to make sure it does not already contain that number, if it does, just generate another.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
As each number is generated, check the current numbers array to make sure it does not already contain that number, if it does, just generate another.
I would like to add another suggestion. If I understood correctly, you have a pot with 49 numbers and you want to randomly select 6. When you select the 1st there are 48 left from which you want to select 5.
So I don't think it is 100% right to select again a random number from 1 to 49 and if it like the one already selected, to generate another. Because you want after the 1st selection the remaining 48 numbers to have the same possibility to be selected.
So here is my suggestion:
Have a Vector with 49 numbers and always generate a random number from 0 to Vector.size(49). Once generated, remove that number from the Vector and generate again another random number from 0 to Vector.size(48).
The number generated will be actually the index of the element in the Vector, so after a few loops they won't match (The number at index 3 will not be the number '3') but that is not important because the index and the selection will be random so you don't care how the numbers are sorted in the Vector.
Another way is to use an array of 49 numbers and when one of them is selected, "swap" it with the last element and randomly take a number from the first 48:
N = 48
ARRAY: {1,2,....49}
loop
take a random index from 0 to N
number = ARRAY[index]
swap the number at the "index" with the number at the "N" position
N = N-1
end loop
Of courseEzzaral's suggestion is much simpler and easier to understand and if your teacher will not give any trouble try Ezzaral suggestion.
PS: Where are you from OracleLite because that lottery is exactly the Lotto we have in my country Greece (49 numbers and 6 are selected)
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448