Autor Beitrag
BerTeL
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 45

Windowx XP
Delphi 7 Enterprise
BeitragVerfasst: Sa 02.12.06 23:01 
Moin
Ich habe ein frage (oha)
Ich würde gern z.b. die zahlen 1 - 10 zufällig in 10 labels verteilen. Natürlich könnte ich für jedes label random(9) + 1 nehmen doch dann kann es ja pasieren das die zahl 5 z.b. 2 mal vorkommt oder so. Wie könnte ich das ganze jetzt machen das jede zahl nur einmal vorkommt?
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Sa 02.12.06 23:01 
Einfach gucken obs schonmal vorgekommen is unn wenn nich neu wählen! :wink:

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
BerTeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 45

Windowx XP
Delphi 7 Enterprise
BeitragVerfasst: Sa 02.12.06 23:03 
darann hab ich auch schon gedacht ... aber wie guck ich das? muss ich da für jede zahl ne booleanvariable machen und dann überprüfen oO. Oder gehts auch einfacher? Mann könnte sie auch alle in einem string speichern aber wie überprüf ich ob die zahl da drinnen steht oder nicht?
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Sa 02.12.06 23:07 
mach doch ein array[1..10] of boolean auf. Und wenn eine Zahl schon vorkam, machts du halt nochmal random und das dann so lange, bis eine Zahl kam, die noch nicht vorkommt (und brichst diese Versuche ab, wenn alle 10 Zahlen verteilt sind).

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Sa 02.12.06 23:08 
Beides würde gehen. Ich würd einfach ein Array of boolean für die Zahlen nehmen und dann sowas machen:
ausblenden Delphi-Quelltext
1:
2:
3:
zahl:=Random(10)+1;
while (benutzt[zahl]) do zahl:=Random(10)+1;
benutzt[zahl]:=true;


PS: Random(9)+1 erzeugt Zahlen von 1-9 und nicht von 1-10!

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
BerTeL Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 45

Windowx XP
Delphi 7 Enterprise
BeitragVerfasst: Sa 02.12.06 23:11 
Danke für die schnelle antwort ... mir jetzt alles klar.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 03.12.06 01:20 
Hier noch ein deterministischer Algorithmus nach Fisher-Yates:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
Const
 Count = 10;
Var
 A : Array[0..Count-1] of Integer;
 I, J, T : Integer;
begin
 For I := 0 to Count-1 do
  A[I] := I;
 For I := 0 to Count-1 do
 begin
  J := I+random(Count-I);
  T := A[J];
  A[J] := A[I];
  A[I] := T;
 end;
 // A[0]..A[9] enthält nun zufällige Zahlen zwischen 0-9 (Ziehung ohne Zurücklegen)
end;