You'll need to call Randomize at the beginning of your program, and use Random to generate each number.
number := random( 45 ) +1; generate random number in 1..45
You'll also have to decide how to store each lotto sequence. For example, a 6/45 sequence might be:
type
LottoNumber = 1..45;
LottoNumbers = array[ 1..8 ] of LottoNumber; // 6 + 2 supplimentary
You can then write a function that generates the lotto numbers using something along the lines of the following pseudo-code:
for each number to generate
repeat
num := generate a random number in 1..high(LottoNumber)
until num not in numbers[ 1..i-1 ]
numbers[ i ] := num
end for
You'll have to figure out how to write the code that does it, but the above algorithm should be your guide.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
You are close, but there are a couple of things you need to fix before it will work right.
The main problem is that you forgot to adjust for the random function. You'll get a number from 0 to 44 inclusive from random( 45 ) , so you can't forget to add one to it.
Callingrandomize should occur only once in the code, otherwise you are defeating the random number generator's randomness. Good places to put it are: in the DPR, in the main form constructor/OnCreate event, and in the main form's initialization section.
Finally, you forgot to free your TStringList. Typically, whenever I create a new string list I automatically add a try..finally block like:
sl := tStringList.create;
try
finally sl.free end;
This guarantees your memory not to leak from the string list.
The use of the string list to track which numbers you already have is a good idea. I also like the use ofTMemo in your OnClick handler. It demonstrates good thinking.
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229