A quick and straightforward tutorial that demonstrates how to generate random numbers in C language.

You can use the same functions to work with C++.

The motivation came from the need of my programming students, especially from courses not directly linked with computer science, such as those of Electrical Engineering.

Yes, I have already taught a lot of C programming logic for Electrical Engineering students 馃榾

@@@@@@@@@ TODO @@@@@@@@@@@@@@@@@

Random numbers in C language#

To generate pseudo-random numbers in C we can use the rand() function from library stdlib.h.

Each function call returns a random number in the closed interval 0..RAND_MAX.

The constant RAND_MAX is set in the interface file stdlib.h.

This function uses a “seed” to generate its random sequence, so if called without the creation of SEED - a different one in each execution, its result will always be the same, that is, will always start with the same “random” number and it will present the same numerical sequence with each call.

So do not forget, always call the rand function after thesrand function.

To generate this seed we call the srand() function by passing a base value as a parameter. This value may be the result obtained from the time() function that returns the value of milliseconds that occurred on the day.

Sample code#

Let’s get down to business, then an algorithm with examples of how to generate random numbers in C language:

#include <stdio.h>
#include <stdlib.h>
int main ( ) {
   int intNumber, x;

   printf("*** Examples of Rand function ***\n\n");

   intNumber=rand();
   printf("integer random without seed generation: %d\n",intNumber);

   srand(25);
   intNumber=rand();
   printf("integer random with fixed seed: %d\n\n",intNumber);

   srand(time(0));
   intNumber=rand();
   printf("integer random with clock-based seed: %d\n\n",intNumber);

   printf("Note that from this point, after the srand function generated the seed, the executions will show different numbers \ n \ n");

   intNumber = rand()%51;
   printf ( "integer from 0 to 50: %d\n\n" , intNumber) ;	// Using the module operator guarantees us that the
         // result of the rest of rand by divisor 51 is a number of
// 0 to 50

   // simulate the rolling of a data of 6 faces
   printf ( "Simulation of 3 plays for a data of 6 faces\n\n") ;

   for (x=1; x<=3; x++) {
      intNumber = (rand()%6)+1; 	// as the number generated by Rand goes from 0 to RAND_MAX, 

      // We generate the result from 0 to 5 and added 1 to ensure the interval from 1 to 6
      printf ("Data rolled with result %d\n", intNumber);
   }

   // simulate three rolls from the face or crown with currency
   printf ( "Face or crown simulation\n\n") ;

   for (x=1; x<=3; x++) {
      intNumber = (rand()%2); 	
      if (intNumber == 0) {
         printf ("cara\n");
      } else {
	printf ("Crown\n");
      }
   }

   getchar() ;
}

If you need to generate truly random numbers a suggestion is to read the article N煤meros aleat贸rios, rand么micos e Pseudo-aleatoriedade, and check the Ramdom.org API service described in this article.