Autor Beitrag
Arne Danikowski
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 194



BeitragVerfasst: Fr 03.07.09 11:28 
Hallo ich möchte eine vierstellige Zufallszahl im Bereich von 0000 - 9999 erzeugen.

Das habe ich bereits:
ausblenden Delphi-Quelltext
1:
2:
3:
Randomize;
for a := 1000 to 9999 do
pass:=(IntToStr(random(9999)));


Funktioniert soweit, jedoch erhalte ich auch immer noch 3 und 2 stellige Zahlen.
Was iat an meinem Code falsch?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 03.07.09 11:37 
Du möchtest eine Zufallszahl erzeugen? Und warum machst du das tausende Male hintereinander in einer Schleife?
ausblenden Delphi-Quelltext
1:
for a := 1000 to 9999 do					

Und du möchtest die im Bereich von 0000 bis 9999 erzeugen? Wie meinst du das? Eine Zahl hat keine führenden Nullen, nur ein String, in dem die Zahl steht, kann das haben.

Meintest du vielleicht das?
ausblenden Delphi-Quelltext
1:
2:
3:
pass := IntToStr(Random(10000));
while Length(pass) < 4 do
  pass := '0' + pass;
Das Randomize sollte nur einmal beim Start des Programms aufgerufen werden, nicht immer wieder.
R4id
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Fr 03.07.09 11:42 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
uses
  Math;

procedure TForm1.FormCreate(Sender: TObject);
var
  MyInt: Integer;
begin
  Randomize;
  MyInt := RandomRange(10009999);

  Caption := IntToStr(MyInt);
end;
Wäre auch eine Lösung.

Gruß R4id
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 03.07.09 11:43 
Sicher, da ginge auch Random(9000) + 1000 (nichts andere macht RandomRange), aber bei dir fehlt die letzte Zahl 9999.

Aber oben steht im Bereich von 0000 bis 9999, also nicht vierstellige Zahlen wie der Titel impliziert, sonder vierstellige Strings. ;-)
Arne Danikowski Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 194



BeitragVerfasst: Fr 03.07.09 11:59 
Vielen dank funktioniert prima!
ffgorcky
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 573

WIN XP/2000 & 7Prof (Familie:Win95,Win98)

BeitragVerfasst: Fr 03.07.09 12:22 
Du dankst doch user profile iconjaenicke für seine Deiner Frage direkt folgenden Antwort - oder?
So hätte ich das zumindest auch gelöst!...
der organist
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 467
Erhaltene Danke: 17

WIN 7
NQC, Basic, Delphi 2010
BeitragVerfasst: Fr 03.07.09 14:01 
ansonsten:

ausblenden Delphi-Quelltext
1:
2:
for i:=0 to 3 do
  pass:=pass+IntToStr(Random(10));


Setzt natürlich voraus, dass pass vorher einmal geleert wird.... Ansonsten mit einmal mit repeat-Schleife:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
repeat
  inc(i);
  pass:=pass+IntToStr(Random(10));
until i=3;


Auch hier natürlich pass vorher leeren und i auf Null setzen...

€: Random (10) statt Random (9)

_________________
»Gedanken sind mächtiger als Waffen. Wir erlauben es unseren Bürgern nicht, Waffen zu führen - warum sollten wir es ihnen erlauben, selbständig zu denken?« Josef Stalin


Zuletzt bearbeitet von der organist am Sa 04.07.09 22:28, insgesamt 1-mal bearbeitet
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Fr 03.07.09 14:11 
Hey,

random(9) erzeugt aber nur Zahlen zwischen 0 und 8!
Besser ist random(10) ;)

MfG Bergmann

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Fr 03.07.09 20:35 
user profile iconder organist hat folgendes geschrieben Zum zitierten Posting springen:
ansonsten:

ausblenden Delphi-Quelltext
1:
2:
for i:=0 to 3 do
  pass:=pass+IntToStr(Random(9));

Was hast du denn damit vor? :shock: Nicht, dass das nicht nur absolut unperformant ist, ich finde es auch wesentlich unverständlicher auf dem ersten Blick als die "einzeilen-Lösung" die vorher bereits erwähnt wurde. Ich plädiere zu user profile iconjaenickes Lösung.

Edit: Pardon, es ging ja um Strings. Das Wetter ... :roll:
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Fr 03.07.09 23:49 
Übrigens würde ich vierstellige Zahlen mit führenden Nullen so formatieren:
ausblenden Delphi-Quelltext
1:
Format('%.4d',[Random(10000)]);					

_________________
Na denn, dann. Bis dann, denn.