I am trying to code a program that will generate six random lotto numbers, and then the two supplementary numbers.
And not to generate the same number twice.

I'm new to pascal, so I understand if you don't want to create a whole code for me but could you at least give me some pointers?

Thanks heaps.

## All 5 Replies

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.

Thankyou soo much!
Heaps of help :)

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.

``````procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
s : String;
sl : Tstringlist;
begin
sl:=TStringList.Create;
Randomize;
while (sl.Count-1) < 7 do begin
i:=random(45);
if sl.IndexOf(intToStr(i))<0 then
end;
Memo1.Clear;
for i := 0 to sl.Count-1 do begin
end;
end;``````

This will return 8 random numbers everytime, I hope this will help

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.

Calling randomize 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 of TMemo in your OnClick handler. It demonstrates good thinking.

Hope this helps.

randomizing and then comparing numbers will solve the problem!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.