La funció rand() en el llenguatge de programació C s'utilitza per generar nombres pseudoaleatoris. S'utilitza en C per generar nombres aleatoris en el rang de 0 a RAND_MAX.
La funció rand() forma part de la biblioteca C estàndard per tant, per utilitzar aquesta funció, hem d'incloure la biblioteca.
Sintaxi de rand()
int rand (void)>
Paràmetres
- Aquesta funció no pren cap paràmetre.
Valor de retorn
- Aquesta funció retorna el valor entre 0 i RAND_MAX, on RAND_MAX és la macro que es defineix a la biblioteca.
Nota: La funció rand() de manera predeterminada utilitza el valor 1 com a llavor per generar números aleatoris que condueixen a la generació de la mateixa seqüència de nombres aleatoris. Per evitar-ho, podem utilitzar srand () funció per especificar una nova llavor per a la funció rand().
Exemples de rand() a C
Exemple 1: Il·lustració de la funció rand().
C
// C program to illustrate the use of rand() function> #include> #include> > int> main()> {> >// Generate a random number using the rand() function> >int> value =>rand>();> > >// Print the generated random value> >printf>(>'The Random Value is: %d'>, value);> > >return> 0;> }> |
>
què és jquery
>Sortida
The Random Value is: 1804289383>
Complexitat temporal : O(1)
Complexitat espacial: O(1)
Exemple 2: genera 10 nombres aleatoris
C
// C program to generate 10 random number using rand()> // function> #include> #include> > int> main()> {> >int> i = 0;> >// Loop through 10 times> >for> (; i <10; i++) {> >// Generate a random number using the rand()> >// function> >int> value =>rand>();> > >// Print the generated random value> >printf>(>'%d '>, value);> >}> > >return> 0;> }> |
>
>Sortida
1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421>
Però, si volem generar alguns nombres aleatoris en l'interval de 0 a N, llavors hem de modificar-los imprimint el mòdul de (N+1) dels nombres que es generen per la funció rand().
Exemple: genera nombres aleatoris més petits que N
C
// C program to generate random numbers smaller than a> // particular number n> #include> #include> int> main()> {> >// Set a constant 'N' to 1000> >int> N = 1000;> > >// Loop through 10 times> >for> (>int> i = 0; i <10; i++) {> >// Generate a random number between 0 and N using> >// the rand() function> >int> value =>rand>() % (N + 1);> >// Print the generated random value> >printf>(>'%d '>, value);> >}> > >return> 0;> }> |
>
>Sortida
897 802 765 992 1 521 220 380 729 969>
D'aquesta manera, podem generar fàcilment el nombre aleatori de 0 a N.
A més, si volem un nombre aleatori que existirà entre a cota inferior i límit_superior , ho podem fer utilitzant matemàtiques simples sobre el nombre aleatori generat per la funció rand().
Exemple: generar nombres aleatoris dins d'un rang
C
// C program to illustrate how to generate random number> // withing a range> #include> #include> int> main()> {> >// Set the upper bound for random numbers> >int> upper_bound = 1000;> >// Set the lower bound for random numbers> >int> lower_bound = 100;> > >// Loop through 10 times> >for> (>int> i = 0; i <10; i++) {> >// Generate a random number within the specified> >// bounds> >int> value =>rand>() % (upper_bound - lower_bound + 1)> >+ lower_bound;> >// Print the generated random value> >printf>(>'%d '>, value);> >}> > >return> 0;> }> |
>
>Sortida
943 897 704 678 132 783 902 760 689 765>