Autor Beitrag
freedy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 403
Erhaltene Danke: 1

Winows 7
Delphi XE
BeitragVerfasst: Mi 08.03.06 14:58 
Hallo,

für einen mathematischen Algorithmus brauche ich möglichst viele Zahlen (> 20.000.000). Da ich noch keine "echten" Daten habe, wollte ich gerne Zufallszahlen generieren.

Dabei stosse ich auf folgende Probleme:

1. wie können negative Zufallszahlen erzeugt werden?
2. warum werden keine Nachkommastellen erzeugt?

Wer kann mir helfen?

Gruß
freedy
Passi077
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 125

Win XP
D7 Pers
BeitragVerfasst: Mi 08.03.06 15:04 
Hi,

hier ein Beispiel zum generieren einer Zahl mit 2 Nachkommastellen zwischen -50 und 50:

Zahl := (Random(10000)-5000)/100;

Grüße
Passi
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mi 08.03.06 15:06 
Hallo,

etwas allgemeiner.

realVar := random; erzeugt ZufallsZahlen aus [0.0..1.0[
Fuer ZufallsZahlen aus Von..Bis funktioniert dies
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
const 
 von = -1000.37;
 bis = 4711.0815;
 Spannweite = bis-von;
var
  i : integer;
  randZahl : double;
begin
randomize;
For i := 0 to 20000000 do
  begin
  randZahl := random*Spannweite+von;
  {Nutze randZahl}
  end;
end.


Gruss Horst
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 08.03.06 20:10 
ausblenden Delphi-Quelltext
1:
2:
Randomize;
MyZufallszahl := RandomRange(-5000, 5000) / 100;

Entspricht Passi077s Methode.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)