C Dev Random

The C Standard does not guarantee the quality of the random sequence produced. In the past, some implementations of rand had serious issues in distribution and randomness of the generated numbers. The usage of rand is not recommended for serious random number generation needs, like cryptography. Pseudo-Random Number Generator (PRNG) In C. In general, a pseudo-random number generator (PRNG) can be defined as a program that takes a seed or a starting number and transforms it into some other number that is different from seed using mathematical operations. Compatibility In C, the generation algorithm used by rand is guaranteed to only be advanced by calls to this function. In C, this constraint is relaxed, and a library implementation is allowed to advance the generator on other circumstances (such as calls to elements of random). If you really need actual random numbers and are on a Linux or BSD-like operating system, you can use the special device files /dev/random and /dev/urandom. These can be opened for reading like ordinary files, but the values read from them are a random sequence of bytes (including null characters). A typical use might be.

  • The C Standard Library
  • C Standard Library Resources
  • C Programming Resources
  • Selected Reading
Random

Description

The C library function void srand(unsigned int seed) seeds the random number generator used by the function rand.

Declaration

Following is the declaration for srand() function.

Parameters

  • seed − This is an integer value to be used as seed by the pseudo-random number generator algorithm.

Return Value

This function does not return any value.

Example

The following example shows the usage of srand() function.

Let us compile and run the above program that will produce the following result −

stdlib_h.htm

# Basic Random Number Generation

The function rand() can be used to generate a pseudo-random integer value between 0 and RAND_MAX (0 and RAND_MAX included).

srand(int) is used to seed the pseudo-random number generator. Each time rand() is seeded wih the same seed, it must produce the same sequence of values. It should only be seeded once before calling rand(). It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.

Standard practice is to use the result of time(NULL) as a seed. If your random number generator requires to have a deterministic sequence, you can seed the generator with the same value on each program start. This is generally not required for release code, but is useful in debug runs to make bugs reproducible.

It is advised to always seed the generator, if not seeded, it behaves as if it was seeded with srand(1).

Possible output:

Notes:

The C Standard does not guarantee the quality of the random sequence produced. In the past, some implementations of rand() had serious issues in distribution and randomness of the generated numbers. The usage of rand() is not recommended for serious random number generation needs, like cryptography.

# Permuted Congruential Generator

Here's a standalone random number generator that doesn't rely on rand() or similar library functions.

Why would you want such a thing? Maybe you don't trust your platform's builtin random number generator, or maybe you want a reproducible source of randomness independent of any particular library implementation.

This code is PCG32 from pcg-random.org, a modern, fast, general-purpose RNG with excellent statistical properties. It's not cryptographically secure, so don't use it for cryptography.

And here's how to call it:

# Restrict generation to a given range

Usually when generating random numbers it is useful to generate integers within a range, or a p value between 0.0 and 1.0. Whilst modulus operation can be used to reduce the seed to a low integer this uses the low bits, which often go through a short cycle, resulting in a slight skewing of distribution if N is large in proportion to RAND_MAX.

The macro

produces a p value on 0.0 to 1.0 - epsilon, so

will set i to a uniform random number within the range 0 to N - 1.

Unfortunately there is a technical flaw, in that RAND_MAX is permitted to be larger than a variable of type double can accurately represent. This means that RAND_MAX + 1.0 evaluates to RAND_MAX and the function occasionally returns unity. This is unlikely however.

C Dev Random Number

# Xorshift Generation

A good and easy alternative to the flawed rand() procedures, is xorshift, a class of pseudo-random number generators discovered by George Marsaglia. The xorshift generator is among the fastest non-cryptographically-secure random number generators. More information and other example implementaions are available on the xorshift Wikipedia page

Example implementation

# Remarks

Due to the flaws of rand(), many other default implementations have emerged over the years. Among those are:

C Dev Random Number

  • arc4random() (available on OS X and BSD)
  • random() (available on Linux)
  • drand48() (available on POSIX)