This library provides both hardware and software random number generation.
Hardware based generation uses an asynchronous oscillator in the xcore device.
To use the module you need to use lib_random in your application CMakeLists.txt, for example:
set(APP_DEPENDENT_MODULES "lib_random")
An application should then the random.h header file:
#include "random.h"
An example demonstrating how to generate random values using the lib_random library is provided
in examples/app_random
It shows the two different methods for initialising a random number generator (software or hardware seed), and then also shows how to generate either a single random value or populate an array with random values.
To build and run the example, run the following from an XTC tools terminal:
cd examples/app_random
cmake -G "Unix Makefiles" -B build
The application binaries can be built using xmake:
xmake -C build
To run the application using the simulator, run the following command:
xsim bin/app_random.xe
The random data values will be printed in the terminal.
There are two random-number APIs available, one API that creates fast pseudo-random numbers using a linear-feedback-shift register, one that slowly creates random bits. A third API enables you to switch the ring oscillator off.
The Pseudo random number generator uses a 32-bit LFSR to generate a pseudo random string of random bits. This has known weaknesses but is exceedingly fast. It comprises the following functions:
Function that creates a random number generator from a seed.
seed – seed for the generator.
a random number generator.
Function that attempts to create a random number generator from a ring-oscillator random value into the seed, using an asynchronous timer. This is based on a 16-bit start value. For better randomness, initialise the random number by calling random_ro_get_bits() 32 times.
a random number generator.
Function that produces a random number. The number has a cycle of 2^32 and is produced using a LFSR.
g – the used generator to produce the seed.
a random 32 bit number.
This interface uses the on-chip ring oscillators to create a random bit
after some time has elapsed. These bits are notionally true random. The bit
rate is limited by a constant RANDOM_RO_MIN_TIME_FOR_ONE_BIT. The
default value is a safe value that should produce random bits in most
circumstances. You can lower it in order to generate more random bits per
second at a risk of introducing correlation.
Function that initialises the ring-oscillator random number generator. Call this once before random_ro_get_bit() is called
Function that may produce a random bit using the ring-oscillator.
If a random bit is available, then it returns 0 or 1 at random.
If no random bits are available, then it returns a negative value which is the time in ticks to wait before the next bit is available.
random_ro_init() must be called before invoking this function.
Random bit, or the negated time to wait in ticks.
The random library switches on a ring oscillator on startup. If it is no longer required it can be switched off to save some power.
Function that stops the ring oscillator, slightly reducing overall power consumption.
XMOS XTC Tools Installation Guide
XMOS XTC Tools User Guide
XMOS application build and dependency management system; xcommon-cmake
https://www.xmos.com/file/xcommon-cmake-documentation/?version=latest