Dans cette rubrique, nous découvrirons la fonction aléatoire et comment générer le nombre aléatoire dans le langage de programmation C. Comme nous le savons, la fonction aléatoire est utilisée pour trouver le nombre aléatoire entre deux nombres définis. Dans le langage de programmation C, la fonction aléatoire a deux fonctions intégrées : les fonctions rand() et srand(). Comprenons ces fonctions dans le langage C.
Fonction Rand()
Dans le Langage de programmation C , la fonction rand() est une fonction de bibliothèque qui génère le nombre aléatoire dans la plage [0, RAND_MAX]. Lorsque nous utilisons la fonction rand() dans un programme, nous devons implémenter le stdlib.h fichier d'en-tête car la fonction rand() est définie dans le fichier d'en-tête stdlib. Il ne contient aucun numéro de départ. Par conséquent, lorsque nous exécutons encore et encore le même programme, il renvoie les mêmes valeurs.
Remarque : Si les nombres aléatoires sont générés avec la fonction rand() sans appeler la fonction srand(), elle renvoie les mêmes séquences de nombres à chaque fois que le programme est exécuté.
Syntaxe
int rand (void)
La fonction rand() renvoie les entiers aléatoires dont la plage va de 0 à RAND_MAX. Le RAND_MAX est une constante symbolique qui définit dans le fichier d'en-tête stdlib.h, dont la valeur est supérieure mais inférieure à 32767 selon les bibliothèques C.
concaténer la chaîne Java
Générez les nombres aléatoires à l'aide de la fonction rand()
Écrivons un programme pour obtenir le nombre aléatoire en utilisant la fonction rand().
si
#include #include #include void main() { // use rand() function to generate the number printf (' The random number is: %d', rand()); printf (' The random number is: %d', rand()); printf (' The random number is: %d', rand()); printf (' The random number is: %d', rand()); getch(); }
Sortir
The random number is: 41 The random number is: 18467 The random number is: 6334 The random number is: 26500
Générez 5 nombres aléatoires à l'aide de la fonction rand()
Considérons un programme pour générer 5 nombres aléatoires à l'aide de la fonction rand() dans le langage de programmation C.
aléatoire.c
#include #include int main() { int i; /* It returns the same sequence of random number on every execution of the program. */ printf(' Random Numbers are: '); for (i = 0; i <5; i++) { printf(' %d', rand()); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Random Numbers are: 41 18467 6334 26500 19169 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Random Numbers are: 41 18467 6334 26500 19169 </pre> <p> <strong>3rd execution of the program</strong> </p> <pre> Random Numbers are: 41 18467 6334 26500 19169 </pre> <p>As we can see in the above output, it returns the same sequence of random numbers on every execution of the programming code.</p> <h3>Generate 10 random numbers from 1 to 100 using rand() function</h3> <p>Let's consider a program to find the random number in C using rand() function.</p> <p> <strong>rand_num.c</strong> </p> <pre> #include #include #include int main() { // declare the local variables int i, num; printf (' Program to get the random number from 1 to 100 '); for (i = 1; i <= 100 10; i++) { num="rand()" % + 1; use rand() function to get the random number printf (' %d ', num); getch(); } < pre> <p> <strong>Output</strong> </p> <pre> Program to get the random number from 1 to 100 42 68 35 1 70 25 79 59 63 65 </pre> <h2>srand() function</h2> <p>The srand() function is a C library function that determines the initial point to generate different series of pseudo-random numbers. A srand() function cannot be used without using a rand() function. The srand() function is required to set the value of the seed only once in a program to generate the different results of random integers before calling the rand() function.</p> <h3>Syntax</h3> <pre> int srand (unsigned int seed) </pre> <p> <strong>seed</strong> : It is an integer value that contains a seed for a new sequence of pseudo-random numbers.</p> <h3>Generate the random numbers using srand() function</h3> <p>Let's write a program to get the random numbers using srand() function in C.</p> <p> <strong>srandNum.c</strong> </p> <pre> #include #include #include // use time.h header file to use time int main() { int num, i; time_t t1; // declare time variable printf(' Enter a number to set the limit for a random number '); scanf (' %d', &num); /* define the random number generator */ srand ( (unsigned) time (&t1)); // pass the srand() parameter printf(' '); // print the space /* generate random number between 0 to 50 */ for (i = 0; i <num; i++) { printf( '%d ', rand() % 50); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit for a random number 10 44 32 23 35 6 33 1 4 22 18 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7 </pre> <p>As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.</p> <h3>Generate the random numbers using srand() and time() function</h3> <p>Let's write a program to get the random numbers using srand() with time() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( ' Seed = %d', time(0)); printf( ' Random number = %d', random); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Seed = 1619450091 Random number = 41 </pre> <h3>Get a seeding value and print the random numbers using srand() function</h3> <p>Let's write a program to get the seed value and random numbers using srand() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int count; unsigned int seed; // use for randomize number printf(' Enter the Seeding value: '); scanf(' %u', &seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(' %5d', + (rand () % 6)); if (count 0) print the number in next line puts(' '); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let's create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (' Enter a number to set the limit of random numbers '); scanf ('%d', num); printf (' Enter the maximum number from you want to get the random number: '); scanf ('%d', max); printf (' %d random number from 0 to %d number are: ', num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (' %d ', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let's consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: ?); for (i = 0; i<10; i++) { printf('%f', ((float) rand() rand_max) * f1); printf(' '); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=></pre></num;></pre></=></pre></5;>
2ème exécution du programme :
Random Numbers are: 41 18467 6334 26500 19169
3ème exécution du programme
Random Numbers are: 41 18467 6334 26500 19169
Comme nous pouvons le voir dans la sortie ci-dessus, il renvoie la même séquence de nombres aléatoires à chaque exécution du code de programmation.
Générez 10 nombres aléatoires de 1 à 100 à l'aide de la fonction rand()
Considérons un programme pour trouver le nombre aléatoire en C à l'aide de la fonction rand().
rand_num.c
#include #include #include int main() { // declare the local variables int i, num; printf (' Program to get the random number from 1 to 100 '); for (i = 1; i <= 100 10; i++) { num="rand()" % + 1; use rand() function to get the random number printf (\' %d \', num); getch(); } < pre> <p> <strong>Output</strong> </p> <pre> Program to get the random number from 1 to 100 42 68 35 1 70 25 79 59 63 65 </pre> <h2>srand() function</h2> <p>The srand() function is a C library function that determines the initial point to generate different series of pseudo-random numbers. A srand() function cannot be used without using a rand() function. The srand() function is required to set the value of the seed only once in a program to generate the different results of random integers before calling the rand() function.</p> <h3>Syntax</h3> <pre> int srand (unsigned int seed) </pre> <p> <strong>seed</strong> : It is an integer value that contains a seed for a new sequence of pseudo-random numbers.</p> <h3>Generate the random numbers using srand() function</h3> <p>Let's write a program to get the random numbers using srand() function in C.</p> <p> <strong>srandNum.c</strong> </p> <pre> #include #include #include // use time.h header file to use time int main() { int num, i; time_t t1; // declare time variable printf(' Enter a number to set the limit for a random number '); scanf (' %d', &num); /* define the random number generator */ srand ( (unsigned) time (&t1)); // pass the srand() parameter printf(' '); // print the space /* generate random number between 0 to 50 */ for (i = 0; i <num; i++) { printf( \'%d \', rand() % 50); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit for a random number 10 44 32 23 35 6 33 1 4 22 18 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7 </pre> <p>As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.</p> <h3>Generate the random numbers using srand() and time() function</h3> <p>Let's write a program to get the random numbers using srand() with time() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( ' Seed = %d', time(0)); printf( ' Random number = %d', random); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Seed = 1619450091 Random number = 41 </pre> <h3>Get a seeding value and print the random numbers using srand() function</h3> <p>Let's write a program to get the seed value and random numbers using srand() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int count; unsigned int seed; // use for randomize number printf(' Enter the Seeding value: '); scanf(' %u', &seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(\' %5d\', + (rand () % 6)); if (count 0) print the number in next line puts(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let's create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (' Enter a number to set the limit of random numbers '); scanf ('%d', num); printf (' Enter the maximum number from you want to get the random number: '); scanf ('%d', max); printf (' %d random number from 0 to %d number are: ', num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let's consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: ?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=></pre></num;></pre></=>
Fonction srand()
La fonction srand() est une fonction de bibliothèque C qui détermine le point initial pour générer différentes séries de nombres pseudo-aléatoires. Une fonction srand() ne peut pas être utilisée sans utiliser une fonction rand(). La fonction srand() est nécessaire pour définir la valeur de la graine une seule fois dans un programme afin de générer les différents résultats d'entiers aléatoires avant d'appeler la fonction rand().
Syntaxe
int srand (unsigned int seed)
graine : Il s'agit d'une valeur entière qui contient une graine pour une nouvelle séquence de nombres pseudo-aléatoires.
Générez les nombres aléatoires à l'aide de la fonction srand()
Écrivons un programme pour obtenir les nombres aléatoires en utilisant la fonction srand() en C.
la chaîne est vide
srandNum.c
#include #include #include // use time.h header file to use time int main() { int num, i; time_t t1; // declare time variable printf(' Enter a number to set the limit for a random number '); scanf (' %d', &num); /* define the random number generator */ srand ( (unsigned) time (&t1)); // pass the srand() parameter printf(' '); // print the space /* generate random number between 0 to 50 */ for (i = 0; i <num; i++) { printf( \'%d \', rand() % 50); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit for a random number 10 44 32 23 35 6 33 1 4 22 18 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7 </pre> <p>As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.</p> <h3>Generate the random numbers using srand() and time() function</h3> <p>Let's write a program to get the random numbers using srand() with time() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( ' Seed = %d', time(0)); printf( ' Random number = %d', random); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Seed = 1619450091 Random number = 41 </pre> <h3>Get a seeding value and print the random numbers using srand() function</h3> <p>Let's write a program to get the seed value and random numbers using srand() function.</p> <p> <strong>srand_time.c</strong> </p> <pre> #include #include int main() { int count; unsigned int seed; // use for randomize number printf(' Enter the Seeding value: '); scanf(' %u', &seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(\' %5d\', + (rand () % 6)); if (count 0) print the number in next line puts(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let's create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (' Enter a number to set the limit of random numbers '); scanf ('%d', num); printf (' Enter the maximum number from you want to get the random number: '); scanf ('%d', max); printf (' %d random number from 0 to %d number are: ', num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let's consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: ?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=></pre></num;>
2ème exécution du programme :
Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7
Comme nous pouvons le voir dans la sortie ci-dessus, il renvoie différentes séquences de nombres aléatoires à chaque exécution du code de programmation.
Générez les nombres aléatoires à l'aide des fonctions srand() et time()
Écrivons un programme pour obtenir les nombres aléatoires en utilisant srand() avec la fonction time().
srand_time.c
#include #include int main() { int random = rand(); // assign the rand() function to random variable srand( time(0)); printf( ' Seed = %d', time(0)); printf( ' Random number = %d', random); return 0; }
Sortir
mémoire virtuelle
Seed = 1619450091 Random number = 41
Obtenez une valeur de départ et imprimez les nombres aléatoires à l'aide de la fonction srand()
Écrivons un programme pour obtenir la valeur de départ et les nombres aléatoires à l'aide de la fonction srand().
srand_time.c
#include #include int main() { int count; unsigned int seed; // use for randomize number printf(' Enter the Seeding value: '); scanf(' %u', &seed); srand (seed); // pass parameter // generate random number between 1 to 6 for (count = 1; count <= 1 5="=" 10; ++count) { printf(\' %5d\', + (rand () % 6)); if (count 0) print the number in next line puts(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4 </pre> <p> <strong>2nd execution of the program:</strong> </p> <pre> Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4 </pre> <p> <strong>3rd execution of the program:</strong> </p> <pre> Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3 </pre> <p>As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.</p> <h3>Generate the random number using the random function</h3> <p>Let's create a program to use stadlib header file to get the random number using random function in C.</p> <p> <strong>func.c</strong> </p> <pre> #include #include #include int main() { int i, num, max, temp; printf (' Enter a number to set the limit of random numbers '); scanf ('%d', num); printf (' Enter the maximum number from you want to get the random number: '); scanf ('%d', max); printf (' %d random number from 0 to %d number are: ', num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let's consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: ?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=></pre></=>
2ème exécution du programme :
Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4
3ème exécution du programme :
Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3
Comme nous pouvons le voir dans la sortie ci-dessus, lorsque nous exécutons le même programme encore et encore avec des valeurs de départ différentes, il affiche les différentes séquences d'un nombre aléatoire de 1 à 6.
Générez le nombre aléatoire à l'aide de la fonction aléatoire
Créons un programme pour utiliser le fichier d'en-tête stadlib pour obtenir le nombre aléatoire à l'aide de la fonction aléatoire en C.
fonction.c
#include #include #include int main() { int i, num, max, temp; printf (' Enter a number to set the limit of random numbers '); scanf ('%d', num); printf (' Enter the maximum number from you want to get the random number: '); scanf ('%d', max); printf (' %d random number from 0 to %d number are: ', num, max); randomize(); for (i = 1; i <= num; i++) { temp="random(max)" * use random() function to get the random number printf (\' %d \', temp); print } getch(); < pre> <p> <strong>Output</strong> </p> <pre> Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65 </pre> <h2>Program to generate float random numbers</h2> <p>Let's consider a program to print the float random numbers in C.</p> <p> <strong>random1.c</strong> </p> <pre> #include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: ?); for (i = 0; i<10; i++) { printf(\'%f\', ((float) rand() rand_max) * f1); printf(\' \'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;></pre></=>
Programme pour générer des nombres aléatoires flottants
Considérons un programme pour imprimer les nombres aléatoires flottants en C.
aléatoire1.c
#include #include #include int main() { srand( (unsigned int) time(NULL)); float f1 = 5.0; int i; printf(?Float random numbers are: ?); for (i = 0; i<10; i++) { printf(\\'%f\\', ((float) rand() rand_max) * f1); printf(\\' \\'); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825 </pre> <hr></10;>
10;>=>=>=>5;>