Re: Lottogenerator
28. Feb 2007, 19:26
Delphi-Quellcode:
TLottozahlen = Array[0..48] of Byte;
TLotto = Class
private
FZahlen: TLottoZahlen;
FCount: Integer;
procedure Delete( Index: Integer);
public
constructor Create;
function Ziehen: Byte;
property Count: Integer read FCount;
destructor Destroy; override;
end;
constructor TLotto.Create;
var i: Integer;
begin
inherited Create;
FCount := 49;
for i:= 0 to 48 do
FZahlen[i] := i+1;
end;
procedure TLotto.Delete( Index: Integer);
begin
System.Move(FZahlen[ Index + 1], FZahlen[ Index],
(FCount - Index) * SizeOf(Byte));
dec(FCount);
end;
function TLotto.Ziehen: Byte;
var x: Integer;
begin
x := random(FCount);
Result := FZahlen[x];
Delete(X);
end;
destructor TLotto.Destroy;
begin
inherited Destroy;
end;
Aufrufen:
Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
Lotto := TLotto.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
for i:= 1 to 6 do
Memo1.Lines.Add(IntToStr(Lotto.Ziehen));
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Lotto.Free;
end;
So könnte man es auch machen
Gruß
Neutral General
Michael "Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
|