Most C compilers come with a collection of standard, standardized libraries containing
common operations (display, math calculations, reading and writing to files ...). These
libraries mainly contain functions which can be used by including the header of the
library concerned. For example, to use the `printf()`` function, you must include the`

stdio.h` library:

`#include <stdio.h>`

The `sdtlib.h`

library contains various basic functions (memory allocation,
conversion of variables ...). The functions we'll focus one here are the
`srand ()`

and `rand ()`

functions which generate random numbers (or
more exactly pseudo-random numbers, but we will come back to that).

We will focus on the following elements of the `stdlib.h`

library:

```
// Symbolic constant
#define RAND_MAX
```

```
// Functions
int rand(void);
void srand(unsigned int seed);
```

The `rand ()`

function generates a pseudo-random number between 0 and `RAND_MAX`

.
`RAND_MAX`

is a symbolic constant (or macro) defined in`stdlib.h`

. This
constant contains the maximum value returned by the `rand ()`

function.

Here is a basic example:

```
// Displays the maximum value of the pseudo-random generator
printf ("Maximum value: %d\n", RAND_MAX);
// Pick two random numbers
printf ("A random number: %d\n", rand());
printf ("Another random number: %d\n", rand());
```

If you run the above code multiple times, you will find that it always draw the same numbers.

A processor is a deterministic machine which cannot generate real
random numbers, moreover the general notion of randomness remains debatable. The
numbers generated by the `rand ()`

function are [pseudo-random numbers] (https://en.wikipedia.org/wiki/Pseudorandom_number_generator)
A pseudo-random algorithm generates a sequence of numbers which have certain properties, such as equiprobability. If the previous program still picks
the same numbers, it is because they are the first of the sequence. Function
`srand ()`

for * seed random * allows to define the seed
generator and thus modify the starting point of the sequence. To avoid
always picking the same random numbers, we conventionally use the current time
as seed of the generator:

```
#include <stdlib.h>
#include <time.h>
```

```
// Initialize the seed of the pseudorandom generator
srand(time(NULL));
```

Now, on each run (at least one second apart from the previous one) the program displays different numbers.

Most of the time, we need to pick numbers in a given interval. If we wish
picking a number between 0 and `max`

, the best solution is to use the rest of the
integer division (modulo `%`

):

```
// x is a pseudirandom number between 0 and max included
int x = rand() % (max+1);
```

If you want a lower bound, you have to shift the draw by adding the bound inferior :

```
// x is a pseudorandom number between min and max included
int x = min + rand() % (max + 1 - min);
```

It is also common to have to pick a real number. The trick is to
divide the number generated by `RAND_MAX`

, and thus obtain a
result between 0 and 1:

```
// x is a random floating point number between 0 et 1
float x = (float)rand()/(float)(RAND_MAX);
```

Note the change of type (cast `int`

->`float`

) in order to perform the division on
floating. By dividing `RAND_MAX`

by the bound`max`

, we can obtain the
drawing a real number between 0 and `max`

:

```
// x is a pseudorandom floating point number between 0 and max
float x = (float)rand() / ((float)RAND_MAX/max);
```

Finally, by shifting the interval, we get a floating point number in the interval
[`min`

; `max`

] as seen previously:

```
// x is a pseudorandom floating point number between min and max
float x = min + (float)rand() / ((float)RAND_MAX/(max-min));
```

Write a program that simulates the draw of two 6-sided dice:

Dace 1 : 6 Dace 2 : 1

To assess students, a teacher needs 24 grades in the range [0; 20]. Write a program that generates these 24 notes:

Grade #1 : 9.2 Grade #2 : 13.1 Grade #3 : 1.1 Grade #4 : 13.2 ... Grade #21 : 17.5 Grade #22 : 18.4 Grade #23 : 19.7 Grade #24 : 15.7

Write a function `rangedom(int a, int b)`

which pick an integer in the interval [`a`

; `b`

].
Draw and display 100 random numbers between 8 and 12 included:

8 9 8 11 9 9 12 12 8 8 8 12 12 11 10 11 12 10 11 11 8 11 10 11 10 9 8 10 10 10 12 10 8 9 8 9 10 10 8 12 10 10 9 9 8 11 10 10 10 10 8 10 8 8 11 12 11 8 11 8 12 10 11 12 8 11 8 12 10 10 12 10 8 8 11 10 8 10 9 11 12 10 10 9 12 10 9 10 11 12 11 10 12 9 10 9 9 10 9 12

Write a `randFloat()`

function that pick a pseudo-random number between 0 and 1. Pick
one million numbers and display the average. The average should be close to 0.5:

Average = 0.5003

** Are the standard libraries supplied with all C compilers? **

Check
Well done!
Try again...

** Are the standard C libraries compatible from one compiler to another? **

Check
Well done!
Try again...

**What really does a pseudo-random generator? **

Check
Well done!
Try again...

** What is this line of code for? **

`srand(time(NULL));`

Check
Well done!
Try again...

** What does the following code do? **

`x = 5 + rand()%10;`

Check
Well done!
Try again...

- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 12/07/2022