Mega Code Archive

 
Categories / Delphi / Examples
 

How to create a series of randomnumbers for any Lottery systems

Title: How to create a series of randomnumbers for any Lottery-systems Question: You will create a series of randomnumbers for any Lottery-systems.. Answer: Create Form1 with this Objects Listbox1 with Font Courier New Edit1 with Edit1.Text := 6 number of randomnumbers Edit2 with Edit2.Text := 49 highest randomnumber Edit3 with Edit3.Text := 10 series of randomnumbers Button1 with onClick event Button1Click This will create ten series of 6 randomnumbers every serie between 1 and 49 (included). Double numbers are removed. You can change all three values. // initialize the random generator Randomize; procedure TForm1.Button1Click(Sender: TObject); var MyList: TStringList; Times, I, Number: Integer; cInt, cLen: string; begin // make the button disabled to prevent multiple clicks Self.enabled := False; // convert the highest number Number := StrToInt(Edit2.Text); // this creates the correct format-argument for every // max-numbers (e.g. 49 , 120, 9999 ....) cLen := IntToStr(length(trim(Edit2.text)) + 1); MyList := TStringList.Create; try // first clear the Listbox Listbox1.clear; // here we start a new serie for Times := 1 to StrToInt(Edit3.Text) do begin // we go thru this while-loop until the max-numbers // are created. Not every loop creates an entry // to the list because double numbers are ignored. while MyList.Count // get a new random number I := Random(Number); if (I 0) then begin // cLen has the number of chars from max-number plus one // e.g. // if max-number is 49 cLen is 3 // if max-number is 111 cLen is 4 // if max-number is 9999 cLen is 5 // this formatting is needed for the correct // sorting of all List-Entries cInt := Format('%' + cLen + '.1d', [I]); // here we look at double entries and ignore it if (MyList.IndexOf(cInt) -1) then continue; // now we add a new randomnumber MyList.Add(cInt); end; end; cInt := ''; // max-numbers are created now we sort it MyList.Sort; // and put it all into Listbox for I := 0 to MyList.Count - 1 do cInt := cInt + MyList.Strings[I]; ListBox1.Items.Add(cInt); // clear MyList for the next serie MyList.clear; end; finally MyList.Free; end; // make the button enable for the next click Self.enabled := True; end; Regards Kurt