quarta-feira, 30 de setembro de 2009

Como gerar numeros randomicos para loterias

{Crie um form com os seguintes objetos:
- Listbox1 com a fonte "Courier New"
- Edit1 com Edit1.Text := 6 Numero de Dezenas
- Edit2 with Edit2.Text := 49 Valor Maximo
- Edit3 with Edit3.Text := 10 Numero de Jogos
- Button1 com onClick com o evento Button1Click abaixo

Isto criará 10 serie de jogos
com os numeros entre 1 e 49
Numeros não serão repetidos
vc poderá mudar os tres valores }

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 < StrToInt(Edit1.Text) do
begin
// 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;



//Outra opção sem utilizar componentes visuais é:



type
{array of series of picks, used in Pick function}
TPick = array of array of integer;

function Pick (APicks, AMax, ASeries: integer): TPick;
var
I, J, Index: integer;
PickArray: array of integer;
begin
if (APicks = AMax) then
begin
raise Exception.Create ('Pick: Max available number should be larger than number of picks');
end; {if}
if (APicks < 1) then
begin
raise Exception.Create ('Pick: You should request at least one pick');
end; {if}
if (ASeries < 1) then
begin
raise Exception.Create ('Pick: You should request at least one series');
end; {if}

SetLength (Result, ASeries);
for I := Low (Result) to High (Result) do
begin
{populate AArray }
SetLength (PickArray, AMax);
for J := Low (PickArray) to High (PickArray) do
begin
PickArray [J] := J + 1;
end; {for}

SetLength (Result [I], APicks);
for J := Low (Result [I]) to High (Result [I]) do
begin
Result [I, J] := 0;
while (Result [I, J] = 0) do
begin
Index := Random (AMax);
Result [I, J] := PickArray [Index];
PickArray [Index] := 0;
end; {while}
end; {for J}
end; {for I}
end; {--Pick--}

//Exemplo de Uso

var
APick: TPick;
begin
APick := Pick (6, 49, 10); {we need 10 series of 6/49 numbers}
...

Nenhum comentário:

Postar um comentário