This module contains a set of functions for generating random numbers using /dev/urandom. If it fails to open this it falls back to using the random() system call. Logic should be call random_begin_reading() before using random_read() and call random_end_reading() after to tidy up.
◆ random_begin_reading()
int random_begin_reading |
( |
void |
| ) |
|
Open /dev/urandom.
- Returns
- 0 on success or -1 on failure
Open URANDOM_FILENAME - more efficient to do it first and then use it although should work if opened each time it's used.
◆ random_read()
void random_read |
( |
u_char * |
buf, |
|
|
int |
count |
|
) |
| |
Read random number from system.
- Parameters
-
buf | Pointer to buffer for data. |
count | Number of bytes to read. |
Reads count bytes from URANDOM_FILENAME. If it hasn't already been opened (by calling random_begin_reading()) then tries to open it. Note it's more efficient to call random_begin_reading() first else URANDOM_FILENAME is opened (and closed) each time random_read() is called.
◆ random_end_reading()
void random_end_reading |
( |
void |
| ) |
|
Tidy up after getting random numbers.
Closes the file descriptor for URANDOM_FILENAME if it is still open.
◆ random_8()
uint8_t random_8 |
( |
void |
| ) |
|
Get an 8 bit random number.
- Returns
- 8 bit random number.
Calls random_read() with 1 as the count.
◆ random_16()
uint16_t random_16 |
( |
void |
| ) |
|
Get a 16 bit random number.
- Returns
- 16 bit random number.
Calls random_read() with 2 as the count.
◆ random_32()
uint32_t random_32 |
( |
void |
| ) |
|
Get a 32 bit random number.
- Returns
- 32 bit random number.
Calls random_read() with 4 as the count.
◆ random_64()
uint64_t random_64 |
( |
void |
| ) |
|
Get a 64 bit random number.
- Returns
- 64 bit random number.
Calls random_read() with 8 as the count.