954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pascal- lotto generation program

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.

jessamyryan
Newbie Poster
2 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

Thankyou soo much!
Heaps of help :)

jessamyryan
Newbie Poster
2 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

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
      sl.Add(intToStr(i))
  end;
  Memo1.Clear;
  for i := 0 to sl.Count-1 do begin
    Memo1.Lines.Add(sl.Strings[i]);
  end;
end;


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

brettgg
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

randomizing and then comparing numbers will solve the problem!

alvisonhunter
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You