A true random number generator has three important properties:

- It is
**unbiased**.

All values of whatever sample size is collected are equiprobable. - It is
**unpredictable**.

It is impossible to predict what the next output will be, given all the previous outputs, but not the internal “hidden” state. - It is
**unreproducible**.

Two of the same generators, given the same starting conditions, will produce different outputs.

- A “good” random number generator is
**unbiased**. - A “cryptographically strong” random number generator is
**unpredictable**. - A “true” random number generator, is
**unreproducible**.

Very rarely is a random number generator all of: “good”, “cryptographically strong”, and “true”. This isn't necessarily a bad thing, but it's worth remembering when evaluating claims that one random number generator is “better” than another.

`<stdlib.h>`

provides us with two random number generator functions:

`rand()`

`srand()`

`rand()`

- This function generates a number between 0 and
`RAND_MAX`

. `RAND_MAX`

is a symbolic constant, defined in`<stdlib.h>`

- Every number between 0 and
`RAND_MAX`

has an equal chance of being chosen each time`rand()`

is called.

int number = rand(); printf("%d\n", number);

1804289383

Why is the number so big? Shouldn’t we *randomly* get a small number
every once in a while?

`srand()`

- This function is used to produce a truer sequence of random numbers,
which is achieved by seeding
`srand()`

with a different seed every time it is called. - This function is called only once in a particular program.
**Once!** - After that, call
`rand()`

to get random numbers.

- A popular method for seeding is to use the system clock.
`time(NULL)`

returns the number of seconds since the start of 1970.- Really—the year 1970.
- As of Sunday 5:00ᴘᴍ, there were 1545004828 seconds
since the start of 1970.

srand(time(NULL)); printf("%d\n", rand());

1290407593

- Numbers generated by the random number generators may not necessarily be in the desired range.
- Scaling allows us to restrict the output values to what is needed.

- To use
`rand()`

and`srand()`

to produce values in a particular range, we may generalize:

int number = a + rand() % b;

`a`

is the shifting value (the first number in the desired range of consecutive integers)`b`

is the scaling factor (the width of the desired range of consecutive integers).

- That is, it returns
`b`

different numbers, starting at`a`

. - In other words, it produces b…b+a−1, inclusive.
- Note that b+a is not a possibility.

srand(time(NULL)); for (int row = 0; row < 7; row++) { for (int i = 0; i < 20; i++) printf(" %d", 18 + rand() % 8); printf("\n"); }

19 22 19 20 18 20 19 21 21 19 20 19 21 21 23 23 22 24 18 21 22 19 19 21 25 20 20 23 19 20 23 20 25 25 23 25 19 24 20 22 25 23 23 20 18 21 18 22 19 18 25 23 20 19 18 19 21 20 25 23 23 22 25 22 21 22 21 22 20 23 18 20 20 24 22 20 19 22 24 20 23 24 25 25 25 25 18 20 19 25 25 24 22 25 20 25 21 23 22 24 21 22 18 23 20 22 18 21 19 24 23 24 22 22 23 21 21 23 24 23 23 23 21 19 22 24 18 18 21 22 24 24 19 24 22 21 20 22 25 21

Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity

Colorado State University, Fort Collins, CO 80523 USA

© 2018 Colorado State University

Colorado State University, Fort Collins, CO 80523 USA

© 2018 Colorado State University