Autor Beitrag
GR-Thunderstorm
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 206



BeitragVerfasst: Di 06.06.06 16:43 
Ich habe mir nen kleinen Code gebastelt, der ein Array aus 7 zufälligen Zahlen (1 bis 7) erstellen soll, welche sich nicht wiederholen dürfen. Das klappt aber leider so nicht und manche Zahlen kommen doppelt... :(

ausblenden volle Höhe 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:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
procedure TForm1.FormActivate(Sender: TObject);
var B: Array[1..7] of integer;
    i,d:integer;
begin
randomize;
i:=1;
repeat
begin

B[i]:=random(7+1);
d:=1;

while d < 8 do
begin
if d = i then
begin
d:=d+1;
end;
if B[i]=B[d] then
begin
B[i]:=random(7+1);
end
else
d:=d+1;

end;
i:=i+1;
end;
until i = 7;

edit1.Text:=IntToStr(B[1]);
edit2.Text:=IntToStr(B[2]);
edit3.Text:=IntToStr(B[3]);
edit4.Text:=IntToStr(B[4]);
edit5.Text:=IntToStr(B[5]);
edit6.Text:=IntToStr(B[6]);
edit7.Text:=IntToStr(B[7]);


end;

Auch seltsam ist, dass die 0 in dem Feld vorkommt, obwohl dies doch gar nicht möglich sein dürfte wenn ich "random(7+1)" verwende..

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Di 06.06.06 16:59 
Random(7+1) ist das Selbe wie Random( 8 ). Ich glaube du meinst Random(7)+1 ;)

d sollte immer nur bis i-1 laufen.
randomize nur einmal aufgerufen werden (FormCreate).

sonst kann ich leider auch kein Fehler finden :gruebel: Habs jetzt aber nicht getestet..

Dann noch ein Tipp: Nach Repeat brauchst du kein begin und du solltest den Quelltext besser einrücken. Macht es sonst schwer ihn zu lesen ;)

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
martin_yt
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Di 06.06.06 17:02 
Hallo
es ist wohl nicht unbedingt die performanteste lösung aber so mach ich das immer:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
var Ar:array[1..7]of integer;
    IsUsed:array[1..7] of boolean;
    i:integer;
begin
 for i:=1 to 7 do IsUsed[i]:=false;
  for i:=1 to 7 do
   begin
    repeat
      Ar[i]:=random(7)+1;
    until IsUsed[Ar[i]]=false;
      IsUsed[Ar[i]]:=true;
   end;
end;

das mit dem random(7+1) is ja schon gesagt worden
fidionael
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 232

Win XP SP2, Ubuntu 6.06
Delphi 7 PE, Delphi 3 Prof
BeitragVerfasst: Di 06.06.06 17:06 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
var B: Array[1..7] of integer;
    i,d:integer;
begin
  Randomize;

  for i:=1 to 7 do begin
    B[i]:=Random(8);
    d:=1;

    while d < 8 do begin
      if d = i then inc(d);
      if B[i]=B[d] then
        REPEAT
          B[i]:=Random(8);
        UNTIL B[i]<>B[d]
      else inc(d);
    end;

    TEdit(FindComponent('Edit'+IntToStr(i))).Text:=IntToStr(B[i]);
  end;
end;


Der Fehler bei dir war, dass du wenn eine Zahl doppelt vorhanden warst du diese zwar geändert hast, jedoch die neue Zufallszahl nicht darauf überprüfst, ob sie vielleicht wieder doppelt vorhanden ist.

Mfg
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Di 06.06.06 17:11 
edit: ... da war mein vorredner schneller...

_________________
Delphi: XE - OS: Windows 7 Professional x64


Zuletzt bearbeitet von Horschdware am Di 06.06.06 17:13, insgesamt 1-mal bearbeitet
GR-Thunderstorm Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 206



BeitragVerfasst: Di 06.06.06 17:13 
Schonmal Danke soweit an alle für die schnellen antworten!


Hab jetzt mal das von fidionael probiert, aber da kommen immernoch Zahlen doppelt vor...
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 06.06.06 17:30 
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:
type TDynIntArray = Array of Integer;

Procedure RandomNumbersKofN(K, N: Integer): TDynIntArray;
var
    Tmp: TDynIntArray;
    I: Integer;
Begin
    If (K > N) OR (N < 1) THEN
        Raise Exception.Create('Ungültige Kombination der Eingabe-Parameter');

    SetLength(Tmp, N);
    For I := 0 TO N-1 do
        Tmp[I] := I;

    SetLength(Result, K);
    For I := K-1 downto 0 do
    Begin
        Result[K] := Tmp[Random(N)];
        Dec(N);
        Tmp[Result[K]] := Tmp[N];
    end;
end;


Für Lotto-Zahlen einfach mit RandomNumbersKofN(6, 49) aufrufen und zu jeder Zahl im Result-Array eins addieren.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.


Zuletzt bearbeitet von BenBE am Di 06.06.06 17:33, insgesamt 1-mal bearbeitet
martin_yt
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Di 06.06.06 17:31 
@ fidionael
hast du den quelltext mal ausprobiert?
der liefert mir nämlich doppelte werte und auch '0'en...
Zitat:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
...
for i:=1 to 7 do begin
    B[i]:=Random(8); 
    d:=1;

    while d < 8 do begin
      if d = i then inc(d);
      if B[i]=B[d] then  
        REPEAT
          B[i]:=Random(8);
        UNTIL B[i]<>B[d]  //du stellst hiermit eigentlich doch bloß sicher,
                          // dass die neue B[i] ungleich B[d] mit maxmialem d, also 7, ist!
                          //ob es ungleich B[1...6] ist, ist nicht mehr sicher!

      else inc(d);
    end;
...
martin_yt
Hält's aus hier
Beiträge: 14



BeitragVerfasst: Di 06.06.06 18:27 
hallo BenBE
ich traus mich ja kaum zu sagen, am ende hab ich bloß nen bock geschossen, aber deine prozedur (oder function ;-) )
gibt mir nur ne accesviolation statt lottozahlen zurück.
und nachdem ich sie so abgeändert hab (ich glaub das soll so heißen, oder?)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
 
...
For I := K-1 downto 0 do
    Begin
        Result[I] := Tmp[Random(N)];
        Dec(N);
        Tmp[Result[I]] := Tmp[N];
    end;
...

erhalte ich aber auch doppelte zahlen im oberen zahlen bereich z.B. bei 80 aus 100.
wieso genau, weiß ich aber auch nicht.., weil eigenhtlich schaut das verfaren recht gut aus :?:

@GR-Thunderstorm
hast du dein array mitzufallszahlen schön erstellen können?
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Di 06.06.06 19:06 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure MachEinArray;
var i, j : integer;
    MyArray : array[1..7] of integer;
begin
  for i:=1 to 7 do
  begin
    MyArray[i]:=random(7)+1;

    j:=1;
    while (j<i) do
    if MyArray[j]=MyArray[i] then
    begin
      MyArray[i]:=random(7)+1;
      j:=1;
    end
    else
      inc(j);

  end;
end;


Das sollte funktionieren

_________________
Delphi: XE - OS: Windows 7 Professional x64
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 06.06.06 19:07 
k, stimmt, hatte noch nen kleinen Logik-Fehler drin. Hier der Fix, dann geht's.

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:
type TDynIntArray = Array of Integer;

Function RandomNumbersKofN(K, N: Integer): TDynIntArray;
var
    Tmp: TDynIntArray;
    I: Integer;
    J: Integer;
Begin
    If (K > N) OR (N < 1) THEN
        Raise Exception.Create('Ungültige Kombination der Eingabe-Parameter');

    SetLength(Tmp, N);
    For I := 0 TO N-1 do
        Tmp[I] := I;

    SetLength(Result, K);
    For I := K-1 downto 0 do
    Begin
        J := Random(N);
        Result[I] := Tmp[J];
        Dec(N);
        Tmp[J] := Tmp[N];
    end;
end;


Und hier, die Generierung der Lottozahlen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.Button1Click(Sender: TObject);
var
    O: TDynIntArray;
    X: Integer;
begin
    O := RandomNumbersKofN(6, 49);
    Label1.Caption := '';
    For X := Low(O) To High(O) Do
    Begin
        Label1.Caption := Label1.Caption + ' ' + IntToStr(O[X]+1);
    end;
end;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Di 06.06.06 19:27 
Ok, bin wieder zuhause.
Jetzt einmal in meinen snippets nachgeschaut und nen alten code von mir gefunden.
einfach, unkompliziert und funktional:
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 FillArray(var a : array of integer; seed : integer; AllowZero : boolean = false);
var i, j, zeroadd : integer;
begin
  if AllowZero then
    zeroadd:=0
  else
    zeroadd:=1;

  for i:=Low(a) to High(a) do
  begin
    a[i]:=random(seed)+zeroadd;

    j:=Low(a);

    while (j<i) do
    if a[j]=a[i] then
    begin
      a[i]:=random(seed)+zeroadd;
      j:=Low(a);
    end
    else
      inc(j);
  end;
end;

_________________
Delphi: XE - OS: Windows 7 Professional x64
GR-Thunderstorm Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 206



BeitragVerfasst: Di 06.06.06 23:10 
Ich bekomme keine einzige dieser Proceduren in Delphi zum laufen.. :(

Bei dem von Horschdware kommen jedesmal exakt die gleichen zahlen raus.. -> [1|7|2|5|3|4|6] :?!?:
chriss1988
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Mi 07.06.06 08:12 
versuchs mal damit


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TForm1.Button1Click(Sender: TObject);

 var Zahlen: Array[1..7] of Boolean;
     i, z: Integer;
begin
  for i:= 1 to 7 do Zahlen[i]:=false;
  for i:=1 to 5 do begin          //die zahl nach dem to sagt dir wieviel zahlen er im edit ausgeben soll
   repeat
    z:=random(7)+1
   until Zahlen[z] = false;
   Zahlen[z]:=true;
  end;
  Edit1.Text:='';
  for i:=1 to 7 do
   if Zahlen[i] then Edit1.Text:=Edit1.Text+' '+Inttostr(i)+' ';
end;
fidionael
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 232

Win XP SP2, Ubuntu 6.06
Delphi 7 PE, Delphi 3 Prof
BeitragVerfasst: Mi 07.06.06 08:37 
Hallo,

ich habe die Prozedur von user profile iconHorschdware auch mal ausprobiert und sie funktioniert .

Also klar, sie spuckt zahlenmäßig immer das gleiche aus, weil du ja in einem Feld der Größe 7 zufällige Zahlen von 1 - 7 erzeugen lässt und keine doppelt vorkommen darf, aber die Reihenfolge der Zahlen ist definitiv zufällig ;)

Ich weiß allerdings auch was du falsch gemacht hast: Du musst in deinem FormCreate-Event noch Randomize; hinzufügen. Wenn ich das entferne krieg ich genau denselben Fehler wie du :)

Mfg
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Mi 07.06.06 08:43 
Hallo!

Es gibt noch einen anderen, relativ einfachen Ansatz:

- Erstelle ein Array mit N Zahlen und fülle es fortlaufend von 1 bis N.
- Lasse ein paar mal (mindestens N-mal) folgenden Code drüberlaufen:
- x:=Random(N); y:=Random(N); =zufällige Indices des Arrays.
- Tausche Arrayelement x mit y aus.

geht am einfachsten. Ist aber nicht als Kritik zu verstehen, nur als Vorschlag. :-)

Ich habe dieses Verfahren seinerzeit verwendet, um eine Bitmap in eine Form zufällig gerastert einblenden zu lassen - sah prima aus. Dabei hatte ich zuerst eine Liste aller Punktkoordinaten fortlaufend erzeugt und dann meinen "Verwürfeler" drübergesetzt.

Claus.
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mi 07.06.06 08:50 
lol
ICH hab dabei keinen fehler gemacht.
das randomize gehört ja nicht in die funktion in der random() verwendet wird.
und da ich nur die funtkion gepostet hab... ^^

kleiner tip:

zur sicherheit empfiehlt es sich folgendes noch einzubauen:
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:
25:
26:
27:
procedure FillArray(var a : array of integer; seed : integer; AllowZero : boolean = false);
var i, j, zeroadd : integer;
begin
  if (High(a) > seed) then
    exit;


  if AllowZero then
    zeroadd:=0
  else
    zeroadd:=1;

  for i:=Low(a) to High(a) do
  begin
    a[i]:=random(seed)+zeroadd;

    j:=Low(a);

    while (j<i) do
    if a[j]=a[i] then
    begin
      a[i]:=random(seed)+zeroadd;
      j:=Low(a);
    end
    else
      inc(j);
  end;
end;


vermeidet eine mögliche endlosschleife

edit: na prima... der alt bekannte highlight bug.
ihr wisst ja was dazugekommen ist

_________________
Delphi: XE - OS: Windows 7 Professional x64


Zuletzt bearbeitet von Horschdware am Mi 07.06.06 08:53, insgesamt 1-mal bearbeitet
fidionael
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 232

Win XP SP2, Ubuntu 6.06
Delphi 7 PE, Delphi 3 Prof
BeitragVerfasst: Mi 07.06.06 08:53 
Ich hab ja auch nicht dich gemeint mit dem Fehler sondern user profile iconGR-Thunderstorm :D
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Mi 07.06.06 08:57 
ah, ok... hab dann wohl was verwechselt.

@logikmensch:
ist natürlich auch eine idee. finde ich an sich gar nicht mal so schlecht.

_________________
Delphi: XE - OS: Windows 7 Professional x64
Logikmensch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 390

Win XP
Delphi 2007 Prof., XE2, XE5
BeitragVerfasst: Mi 07.06.06 09:00 
Hallo!

Danke. Und ja: Es funzt immer und ich brauche keine lästigen Tests. Okay, man kann bei meiner Methode vielleicht noch sicherstellen, dass X<>Y ist. Okay... :-)

Claus.