Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

srand () i rand ()

[es] :: C/C++ programiranje :: srand () i rand ()

[ Pregleda: 5877 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

laly

Član broj: 11430
Poruke: 3
*.net.hinet.hr

ICQ: 104797049


Profil

icon srand () i rand ()20.06.2003. u 02:50 - pre 252 meseci
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!
Kiss!
 
Odgovor na temu

bokash

Član broj: 658
Poruke: 35
212.200.84.*

Sajt: bokash.co.yu


Profil

icon Re: srand () i rand ()20.06.2003. u 04:33 - pre 252 meseci
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
pozdrav od Borisa
 
Odgovor na temu

Dejan Lozanovic
Dejan Lozanovic
Beograd

Član broj: 691
Poruke: 2325
*.verat.net

Jabber: null@elitesecurity.org
Sajt: speedy-order.com


+75 Profil

icon Re: srand () i rand ()20.06.2003. u 14:16 - pre 252 meseci
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)."

 
Odgovor na temu

zeco

Član broj: 5873
Poruke: 9
*.net4u.hr



Profil

icon Re: srand () i rand ()21.06.2003. u 17:58 - pre 252 meseci
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))
 
Odgovor na temu

laly

Član broj: 11430
Poruke: 3
*.net.hinet.hr

ICQ: 104797049


Profil

icon Re: srand () i rand ()23.06.2003. u 01:38 - pre 252 meseci
Thanx!
Nesto sam shvatila!
Kiss!
 
Odgovor na temu

[es] :: C/C++ programiranje :: srand () i rand ()

[ Pregleda: 5877 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.