A quick and straight to the point tutorial that demonstrates how to work with and generate random numbers - random, in `Ç`

.

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

.

The motivation came from the need of my programming apprentices, mainly from courses not directly related to informatics, such as Electrical Engineering.

*yes, I have taught a lot of Programming Logic in C to Electrical Engineering students 😀*.

read Random, Random and Pseudo-random numbers – fundamental concepts about generating random numbers

## Random numbers, random in C

To generate pseudorandom numbers in C we can use the function `rand()`

from the library **stdlib.h**.

Each function call produces a random number in the closed range **0..RAND_MAX.**

the constant **RAND_MAX** is defined in the interface file **stdlib.h**.

This function uses a “seed” to generate its random sequence, so if called without creating a *seed* – a seed, different at each execution, its result will always be the same, that is, it will always start with the same “random” number and will present the same numerical sequence at each call, so don't forget, always call the function `rand`

after function `srand`

.

*To be mnemonic, rand is the abbreviation of “random” which means random in English. The srand function would be the abbreviation for “random seed” or random seed – in literal translation.*

To generate this seed we call the function `srand()`

passing a base value as a parameter. This value can be the result obtained from the function `team()`

which returns the value of milliseconds that occurred in the day.

### sample code

Let's get down to business, here's an algorithm with examples of how to generate random numbers in c language:

```
#include #include int main() { int integer, x; printf("*** EXAMPLES FOR USING RAND FUCTION ***\n\n"); integer=rand(); printf("random integer without seed generation: %d\n",integer); srand(25); integer=rand(); printf("random integer with fixed seed: %d\n\n",integer); srand(time(0)); integer=rand(); printf("random integer with clock based seed: %d\n\n",integer); printf("Note that from this point on, after the srand function has generated the seed,\n executions will show different numbers\n\n"); integer = rand(); printf("integer from 0 to 50: %d\n\n" , integer) ; // using the modulo operator guarantees that the // result of the remainder of rand by divisor 51 is a number from // 0 to 50 // simulate the roll of a 6-sided die printf ("Simulation of 3 rolls for a 6-sided die\n\n" ); for (x=1; x<=3; x++) { integer = (rand()%6)+1; // as the number generated by rand goes from 0 to RAND_MAX, // we generate the result from 0 to 5 and add 1 to guarantee the range from 1 to 6 printf ("Dice rolled with result %d\n", integer); } // simulate three flips of heads or tails with coin printf ("Simulation of heads or tails\n\n" ); for (x=1; x<=3; x++) { integer = (rand()%2); if (integer == 0) { printf("Face\n"); } else { printf("Crown\n"); } } getchar() ; }
```

If you need to generate truly random numbers a suggestion is to read the article Random, Random and Pseudo-random numbers, and check the RAMDOM.ORG API service described in this article.

**keep up to date**

Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.

Pingback: Random, Random and Pseudo-Random Numbers - MAZER.DEV