Autor Beitrag
teamrocket0
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 177
Erhaltene Danke: 1

Win ME, Win XP, Win 7, Win 10
Delphi 7, 10.2 Tokyo
BeitragVerfasst: Fr 26.01.07 17:58 
Moin.

Meine Übung vom letzen mal:
www.delphi-forum.de/...amp;highlight=random

hat einen kleinen Bug. Ich habe ein MaskEdit eingebaut mit dem man die Anzahl der Durchläufe einstellen kann. Maximal 99 sind möglich.

Es kann ja vorkommen das aus dem "Random-Bereich" ein Eintrag zwei mal oder öfter aufgelistet wird.

Nur das kann ich leider nicht gebrauchen.
Wie kann ich dafür sorgen das jede Zeile bei einem Arbeitsgang (Also 1- max. 99 Durchlaufe) nur einmal auftritt. Wir gehen mal davon aus das der "Random-Bereich" groß genug ist.

Vielen Dank für einen Hinweiß!
Robinator
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 275

WinXP
BDS 2006
BeitragVerfasst: Fr 26.01.07 18:02 
Du könntest dir nen Array of integer erstellen, in das du eine zahlenfolge von 0..X einträgst. Nun könntest du Per Random(Length(MyArr)) darauf zugreifen und den entsprechenden eintrag danach löschen. So kann jeder Wert nur einmal vorkommen.

Gruss, rob
teamrocket0 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 177
Erhaltene Danke: 1

Win ME, Win XP, Win 7, Win 10
Delphi 7, 10.2 Tokyo
BeitragVerfasst: Fr 26.01.07 18:06 
Bohr.

Kling Hammer komplieziert.
Ich hätte mir jetzt einfach mal 99 Variablen erstellt und mit einer Zählschleife immer eine Variable nach der anderen mit dem Randomwert beschrieben und danach abgefrag ob der neue Randomwert irgendwo einaml in einer Variable auftaucht!
Robinator
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 275

WinXP
BDS 2006
BeitragVerfasst: Fr 26.01.07 18:16 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Procedure CreateRandomBla;
var
  MyArr : Array of Integer;
  RandArr : Array[0..X] of Integer;
  i, ind : integer;
  Procedure Delete(Index : integer);
  var
   i : integer;
  begin
    for i := index to Length(MyArr) - 2 do
      MyArr[i] := MyArr[i + 1];
    SetLength(MyArr, Length(MyArr) - 1);
  end;
begin
  SetLength(MyArr, X + 1);
  for i := 0 to X do
    MyArr[i] := i;
  for i := 0 to X do 
  begin
    ind := Random(Length(MyArr));
    RandArr[i] := MyArr[ind];
    Delete(ind);
  end;
end;


So, das is doch garnicht alzu kompliziert - hab das jetzt mal eben hier reingetippselt, ist also nicht getestet. Sollte aber so, oder mit kleinen modifikationen laufen. (X musst du natürlich mit der Gewünschten Anzahl ersetzen.)

gruss, rob
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 27.01.07 00:58 
Moin!

Der Vollständigkeit halber: Zufallszahlen im Bereich von x..y erzeugen geht am besten so: ;)
  1. Die Zahlen in ein Array eintragen:
    ausblenden Delphi-Quelltext
    1:
    2:
    for i := x to y do
      Feld[i] := i;

  2. Das Feld nach Suche in: Delphi-Forum, Delphi-Library MILLER YATES "durchschütteln". ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Sa 27.01.07 09:55 
Narses, ich glaube, er möchte N eindeutige Zufallszahlen aus dem Bereich 0..M, wobei M>=N. Insofern ist der Code Robinator im Prinzip zu gebrauchen.

_________________
Na denn, dann. Bis dann, denn.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 27.01.07 17:15 
Moin!

Hmm... :? nach erneutem Drübersehen scheint mir das auch so... :| Also, bischen schnell mit meinem Vorschlag gewesen... ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.