Hi, Ich hab noch ein Problem mit dem Zufallsgenerator: Wenn ich folgenden Code benütze: srand((unsigned int)(abstand*1000)); y=(double)rand()/(double)1000; abstand und y sind double-Variablen abstand wird vorher gesetzt. Wenn ich z.B. nur 10 für abstand nehme bekomme ich für y Werte von bis zu 600000. Wie geht das? Eigentlich sollte y im Bereich 0-abstand liegen. Das *1000 halte ich für notwendig, da für abstand kleine Werte der Normalfall sein werden und ich doch ne recht große Streuung der Werte will. Also wieso kommen da so große Werte für y raus? philipp
On Sun, 28 Dec 2003 at 13:47 (+0100), Philipp Otte wrote:
Ich hab noch ein Problem mit dem Zufallsgenerator:
Wenn ich folgenden Code benütze:
srand((unsigned int)(abstand*1000)); y=(double)rand()/(double)1000; ^^^^^^^^^^^^ 1000.0 würde auch reichen
abstand und y sind double-Variablen abstand wird vorher gesetzt.
rand() liefert Werte im Bereich [0; RAND_MAX]. Das Argument von srand() hat damit nichts zu tun, Du solltest es im übrigen immer mit was anderem füttern sonst bekommst Du keine zufälligen Zahlen. Und Du solltest srand() immer nur einmal im Programm aufrufen. Und vor allem solltest Du, bevor Du eine Funktion verwendest, die Du nicht verstanden hast, die Dokumentation lesen. man rand: [...] DESCRIPTION The rand() function returns a pseudo-random integer between 0 and RAND_MAX. The srand() function sets its argument as the seed for a new sequence of pseudo- random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If no seed value is provided, the rand() function is automatically seeded with a value of 1. RETURN VALUE The rand() function returns a value between 0 and RAND_MAX. The srand() returns no value. [...] Und die Infopage zur libc: ISO C Random Number Functions ----------------------------- This section describes the random number functions that are part of the ISO C standard. To use these facilities, you should include the header file `stdlib.h' in your program. - Macro: int RAND_MAX The value of this macro is an integer constant representing the largest value the `rand' function can return. In the GNU library, it is `2147483647', which is the largest signed integer representable in 32 bits. In other libraries, it may be as low as `32767'. - Function: int rand (void) The `rand' function returns the next pseudo-random number in the series. The value ranges from `0' to `RAND_MAX'. - Function: void srand (unsigned int SEED) This function establishes SEED as the seed for a new series of pseudo-random numbers. If you call `rand' before a seed has been established with `srand', it uses the value `1' as a default seed. To produce a different pseudo-random series each time your program is run, do `srand (time (0))'. Steht doch eigentlich alles drin, was man wissen muss, oder? Gruß, Bernhard -- _________ http://www.bwalle.de _________________________________________________ "Demokratie heißt, sich in die eigenen Angelegenheiten einmischen." -- Max Frisch
participants (2)
-
Bernhard Walle
-
Philipp Otte