Autor Beitrag
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Fr 04.03.05 19:23 
Hallo,

Ich möchte 50 millionen mal würfeln, um die Fehlertoleranz zu berechnen.

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:
formcreate;
begin
 randomize;
end;

for i := 1 to 50000000 do
  begin
   a := random(5)+1;
    case a of
     1 : inc(a);
     2 : inc(b);
     3 : inc(c);
     4 : inc(d);
     5 : inc(e);
     6 : inc(f);
    end;
  if (i mod 5000)=0 then
   begin
    gauge1.Progress := i;
    Gauge1.Refresh;
   end;
  end;


Als ergebniss bekomme ich sowas:

3,4,5 bekommen realistische Werte.
1 und 6 bekommen jeweils 5 und 0...

Verstehe nicht was da schief läuft.

Die Variablen a-f sind deklariert und initalisiert.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Fr 04.03.05 19:25 
Is auch klar, weil es Random(6) + 1 heißen müsste.... ;)

AXMD
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Fr 04.03.05 19:28 
Hallo

die 1:
Zitat:

a := random(5)+1;
case a of
1 : inc(a);

du verwendest a zweimal in deinem code, einmal für die Zufallszahl, und einmal zum zählen, das kann so nicht funktionieren.

damit die 6 richrtig wird, mußt du schreiben random(6)+1.
oh hat folgendes geschrieben:

Random gibt eine Zufallszahl im Bereich 0 <= X < Range zurück.


Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Karlson Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Fr 04.03.05 19:32 
okay, hab das verwechselt. Jetzt ist die 6 schonmal richtig ;)
Die 1 liefert immer noch einen Wert zwischen 3 und 6 zurück...weil ich auf den Mond geschossen gehöre.
Ich trottel habe a zweimal verwendet :roll: Ja, wie man doch rauskommt wenn man 2 Wochen nicht programmiert...