Wt  4.14.0
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
Wt::WRandom Class Reference

Random number generator. More...

#include <Wt/WRandom.h>

Static Public Member Functions

static unsigned int get ()
 Returns a random number.
 
static std::string generateId (int length=16)
 A utility method to generate a random id.
 
static void setGenerator (const std::function< unsigned int()> &generator)
 Sets the random number generator used by WRandom.
 
static void setUniformGenerator (const std::function< int(::int32_t min, ::int32_t max)> &generator)
 Sets the uniform random number generator used by WRandom.
 

Detailed Description

Random number generator.

If an implementation is available for your OS, this class generates high-entropy random numbers, suitable for secret ids (e.g. this is used to generate Wt's session IDs).

Member Function Documentation

◆ generateId()

std::string Wt::WRandom::generateId ( int  length = 16)
static

A utility method to generate a random id.

The id is composed of small and capitalized roman characters and numbers [a-zA-Z0-9].

See also
get()

◆ get()

unsigned int Wt::WRandom::get ( )
static

Returns a random number.

This returns a high-entropy (non deterministic) random number on platforms for which this is supported (currently only Linux, Windows and MacOS X).

◆ setGenerator()

static void Wt::WRandom::setGenerator ( const std::function< unsigned int()> &  generator)
static

Sets the random number generator used by WRandom.

Sets the random number generator used by WRandom::get().

The generator should be thread-safe, as it may be called from multiple threads.

Note
This function is not thread safe, and should not be called concurrently with any function of WRandom.
Warning
Several security features of Wt depend on the quality of the random number generator. Replacing it with a low-entropy or deterministic generator will compromise security.

◆ setUniformGenerator()

static void Wt::WRandom::setUniformGenerator ( const std::function< int(::int32_t min, ::int32_t max)> &  generator)
static

Sets the uniform random number generator used by WRandom.

Sets the uniform random number generator used by WRandom::generateId(). The generator should return a uniformly distributed random number in the range [ min, max ] where min and max are given as parameters to the generator function.

The generator should also be thread-safe, as it may be called from multiple threads.

Note
This function is not thread safe, and should not be called concurrently with any function of WRandom.
Warning
Several security features of Wt depend on the quality of the random number generator. Replacing it with a low-entropy, non-uniform or deterministic generator will compromise security.