Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Zufallszahl [1..9] jede Ziffer min/max einmal (https://www.delphipraxis.net/113031-zufallszahl-%5B1-9%5D-jede-ziffer-min-max-einmal.html)

Deltachaos 1. Mai 2008 14:47


Zufallszahl [1..9] jede Ziffer min/max einmal
 
Ich muss eine zufalls Zahl erzeugen die ungefähr so aussieht.

Code:
143258796
123456789
745632981
Im grundegenommen muss nur die Stelle Zufällig sein.

Wie mache ich so etwas?

mkinzler 1. Mai 2008 14:49

Re: Zufallszahl [1..9] jede Ziffer min/max einmal
 
Schreibe die Ziffern in einen Array und mische ihn zufällig

toms 1. Mai 2008 15:05

Re: Zufallszahl [1..9] jede Ziffer min/max einmal
 
Delphi-Quellcode:
procedure Shuffle(var a: array of Byte);
// Zahlen mischen
var
  i, j, temp: Integer;
begin
  for i := 1 to High(a) do
  begin
    j := Pred(i + Random(Length(a) - i));
    temp := a[Pred(i)];
    a[Pred(i)] := a[j];
    a[j] := temp;
  end;
end;
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  a: array of Byte;
  i: Byte;
  s: string;
begin
  // Länge festsetzen
  setLength(a, 9);

  // Werte 1..9 zuweisen
  for i := 0 to 8 do
   a[i] := i + 1;

  // Random Nummer Generator initialisieren
  Randomize;

  // Mischeln
  Shuffle(a);

  // Zahlen zusammensetzen
  s := '';
  for i := 0 to 8 do
   s := s + Inttostr(a[i]);

  // Zahlen ausgeben
  Caption := s;
end;

Deltachaos 1. Mai 2008 15:28

Re: Zufallszahl [1..9] jede Ziffer min/max einmal
 
thx
:mrgreen: :mrgreen:


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:59 Uhr.

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