libgslutil  1.2.3
Macros | Functions
Generate random numbers

Macros

#define URANDOM_FILENAME   "/dev/urandom"
 Name of urandom device.
 

Functions

int random_begin_reading (void)
 Open /dev/urandom. More...
 
void random_read (u_char *buf, int count)
 Read random number from system. More...
 
void random_end_reading (void)
 Tidy up after getting random numbers. More...
 
uint8_t random_8 (void)
 Get an 8 bit random number. More...
 
uint16_t random_16 (void)
 Get a 16 bit random number. More...
 
uint32_t random_32 (void)
 Get a 32 bit random number. More...
 
uint64_t random_64 (void)
 Get a 64 bit random number. More...
 

Detailed Description

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.

Function Documentation

◆ 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
bufPointer to buffer for data.
countNumber 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.