Random

Interface for creating random numbers and strings.

File information

Filecommon/interface/random.h

Classes IRandom
Data types random_char_t
Examples Creating a random number
Creating a random string

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.

Public functions

Init (static function)
Obsolete! There is no need to call Init anymore, as the platform_random() function now handles this automatically, which is used inside GetRandom().
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
  • a-z
  • A-Z
  • 0-9
  • !
  • _
random_chars_ice
  • a-z
  • A-Z
  • 0-9
  • +
  • /

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_urlA set of characters that doesn't need escaping in URLs. (a-z A-Z 0-9 ! _)
random_chars_iceThe 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);