eli mun pitäis saada arvottuu luku 0-50.. miten onnistuis?
C:llä
#include <math.h>
rand()%51
juu kiitos :)
Muista sitten alustaa satunnaislukugeneraattori ensin. Lisätietoja C-oppaassa.
Taitaa muuten olla #include <stdlib.h>
Minusta randia voi jo pelkällä stdio:lla.
Väärin, TGunner.
man 3 rand kirjoitti:
NAME rand, rand_r, srand - pseudo-random number generator SYNOPSIS #include <stdlib.h> int rand(void); int rand_r(unsigned int *seedp); void srand(unsigned int seed);
jne.
Ja muuten, T.M.:lle:
man 3 rand kirjoitti:
NOTES The versions of rand() and srand() in the Linux C Library use the same random number generator as random() and srandom(), so the lower-order bits should be as random as the higher-order bits. However, on older rand() implementations, and on current implementations on different systems, the lower-order bits are much less random than the higher- order bits. Do not use this function in applications intended to be portable when good randomness is needed. In Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the follow- ing comments are made: "If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0))); and never by anything resembling j = 1 + (rand() % 10); (which uses lower-order bits)."
miksei noiden tagien sisällä voi käyttää b:tä putkassa :(
Aihe on jo aika vanha, joten et voi enää vastata siihen.