[ laly @ 20.06.2003. 02:50 ] @
Tko zna site sa zadacima vezanim uz ove dvije funkcije,please, neka mi ga napise! Pitanje zivota i smrti! Ako ima i nesto teorije,jos bolje! Thanx! |
[ laly @ 20.06.2003. 02:50 ] @
[ bokash @ 20.06.2003. 04:33 ] @
srand zoves samo jedanput kao generator da ti ne bi svaki poziv
rand generisao istu sekvencu. /* RAND.C: This program seeds the random-number generator * with the time, then displays 10 random integers. */ #include <stdlib.h> #include <stdio.h> #include <time.h> void main( void ) { int i; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); } primer za rand /* getrandom returns a random number between min and max, which must be in * integer range. */ #define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min)) jos jedan // return an integral random number in the range 0 - (n - 1) int Rand(int n) { return rand() % n ; } jos jedan RGB (rand () % 256, rand () % 256, rand () % 256) znaci brojevi su izmedju 0 i 255 [ Dejan Lozanovic @ 20.06.2003. 14:16 ] @
Uh pa ne valja da se za interval od 0-(n-1) da se uzima ostatak tj bitovi nize tezine, Vec je bolje uzetri bitove vece tezine za tu svrhu :)
Citat: 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 following 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)." [ zeco @ 21.06.2003. 17:58 ] @
void srand ( unsigned int seed );
Initialize random number generator. Uses seed parameter to set a new starting point for generating random numbers with rand. If seed is set to 1 the generator is reinitialized to its initial value as before any call to rand or srand. In order to generate true random numbers it is suggested to use as seed a value that changes often, like the one returned by time function included in <time.h> (the number of seconds elapsed since newyear 1970). Parameters. seed An integer value to be used as starting point with the pseudo-random number generator algorithm. Return Value. (none) Portability. Defined in ANSI-C. int rand ( void ); Generate random number. Returns a pseudo-random number in the range from 0 to RAND_MAX constant. This is generated by an algorithm that returns a series of non-related numbers each time is called. This algorithm should be initialized to different starting points using function srand to generate more realistic random numbers. RAND_MAX is a constant defined in stdlib.h. Its default value is implementation defined. Parameters. (none) Return Value. An integer value between 0 and RAND_MAX. Portability. Defined in ANSI-C. U slučaju Microsoft Visual C++ RAND_MAX = 32767 Zadatak: /* rand example */ #include <stdio.h> #include <stdlib.h> #include <time.h> int main () { /* initialize random generator */ srand ( time(NULL) ); /* generate some random numbers */ printf ("A number between 0 and RAND_MAX (%d): %d\n", RAND_MAX, rand()); printf ("A number between 0 and 99: %d\n", rand()%100); printf ("A number between 20 and 29: %d\n", rand()%10+20); return 0; } Output: A number between 0 and RAND_MAX (32767): 30159 A number between 0 and 99: 72 A number between 20 and 29: 23 U ovom slučaju uzima uzima zadnji ili zadnja dva broja i pridodaje mu minimalnu vrijednost unutar koje će se nalaziti broj. Drugi način je: (int)((rand()/(RAND_MAX))* (kraj intervala) + (početak intervala)) [ laly @ 23.06.2003. 01:38 ] @
Thanx!
Nesto sam shvatila! Copyright (C) 2001-2025 by www.elitesecurity.org. All rights reserved.
|