Random
Interface for creating random numbers and strings.
File information
Classes
IRandom
class IRandom {
public:
static void Init(dword seed);
static dword GetRandom();
static void GetChars(char * out, dword len, random_char_t type = random_chars_url);
};
Overview
The IRandom interface can be used to create random numbers or strings. Before it can be used, it should be initialized with a seed that should contain some entropy.
Public functions
Init (static function)
Initializes the random number generator. This function should be called once when the app is started.
Parameters
dword seed |
A 32-bit value that should contain some entropy. Make sure that the app does not use the same value over and over again.
|
GetRandom (static function)
Returns a random number.
Return value
dword |
A 32-bit unsigned integer.
|
GetChars (static function)
Writes a number of random characters to a buffer. Please note that the function does not do any null-termination.
A typical application of this function is to create random passwords.
Parameters
char * out |
The buffer to write the random characters to. It must be big enough for len characters.
|
dword len |
The number of random characters that shall be written.
|
random_char_t type |
Optional parameter that specifies set of characters that shall be used.
- random_chars_url
-
- random_chars_ice
-
|
Remarks
For each written character there are 64 different possibilities. So the random string contains only len/4 bytes of randomness.
Data types
random_char_t
enum random_char_t {
random_chars_url,
random_chars_ice
};
Overview
The random_char_t enum defines the different charsets for random strings that are supported by the library.
Values
random_chars_url | A set of characters that doesn't need escaping in URLs. (a-z A-Z 0-9 ! _) |
random_chars_ice | The set of characters needed for ICE/STUN passwords. (a-z A-Z 0-9 + /) |
Code Example
Creating a random number
dword number = IRandom::GetRandom();
Creating a random string
char out[17];
IRandom::GetChars(out, 16, random_chars_url);
out[16] = 0;
debug->printf("random string: %s", out);