Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Random ohne doppelte Zahlen (https://www.delphipraxis.net/121705-random-ohne-doppelte-zahlen.html)

juergen 3. Okt 2008 13:25

Re: Random ohne doppelte Zahlen
 
Hallo,

hier würde sich auch ein Array vom Typ Boolean anbieten.

Pseudocode:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var My_LisboxIndex: Array[1..(Listbox.Count)] of Boolean;
     i, z: Integer;
begin
  for i:= 1 to (Listbox.Count) do My_LisboxIndex[i]:=false;
  for i:=1 to (Listbox.Count) do begin
   repeat
    z:=random(Listbox.Count) + 1
   until My_LisboxIndex[z] = false;
   My_LisboxIndex[z]:=true;
  end;
  for i:=1 to (Listbox.Count)] do
   if My_LisboxIndex[i] then Listbox.TopIndex := (i); //oder halt irgendwas anderes an dieser Stelle tun...
end;

Uwe Raabe 3. Okt 2008 13:40

Re: Random ohne doppelte Zahlen
 
Ich gehe mal davon aus, daß wir 12 Namen haben, die in labelededit1..12 stehen. Dann sähe eine Lösung (analog des Vorschlags von Z4ppy) so aus:

Delphi-Quellcode:
var
  list: TStringList;
  I, N: Integer;
begin
  list := TStringList.Create;
  try
    { Liste mit den Einträgen füllen }
    list.Add(labelededit1.Text);
    ...
    list.Add(labelededit12.Text);
   
    N := list.Count;
    for I:=1 to N do begin
      if list.Count < 2 then
        N := Random(list.Count)
      else
        N := 0;
      memo1.Lines.Add(IntToStr(I) + ':' + list[N]);
      list.Delete(N);
    end;
    Assert(list.Count = 0, 'Liste nicht leer!');
  finally
    list.Free;
  end;
end;

alzaimar 4. Okt 2008 07:15

Re: Random ohne doppelte Zahlen
 
Den Code verstehe ich nicht.

Zitat:

Zitat von SirTwist
In ... "The Art Of Computer Programming" von Donald E. Knuth ... ist beschrieben...

Das ist der Fisher-Yates Algorithmus.


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:46 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz