Class TCastleRandom

Unit

Declaration

type TCastleRandom = class(TObject)

Description

Custom, fast random number generator. Implementation of XorShift algorithm for random numbers generation. In some cases it works 2 to 3 times faster than native FPC random function. It also allows for multiple repeatable random seeds to support parallel pseudo-random sequences. The algorithm should not be used for cryptographic purposes, because it's speed-oriented (with quality ok for game, but not cryptographic use), and might subject to change in future.

Hierarchy

Overview

Methods

Public constructor Create(RandomSeed: LongWord = 0);
Public procedure Initialize(RandomSeed: LongWord = 0);
Public function Random: single; overload;
Public function Random(N: LongInt): LongInt; overload;
Public function RandomInt64(N: int64): int64;
Public function RandomBoolean: boolean;
Public function RandomSign: longint;
Public function Random32bit: LongWord;

Description

Methods

Public constructor Create(RandomSeed: LongWord = 0);

Create and initialize (seed) the random generator. Parameter RandomSeed value 0 indicates to use a random seed (derived from current time and some other paramteres).

Public procedure Initialize(RandomSeed: LongWord = 0);

Initializes current seed. The seed must be a non-zero integer. Provide Zero value to initialize random seed based on current time (CPU ticks) and some other paramteres. This procedure is thread-safe, you'll get different random seeds even if initialization happens absolutely simultaneously.

Public function Random: single; overload;

Returns random float value in the 0..1 range.

Public function Random(N: LongInt): LongInt; overload;

Returns random integer number in the 0..N-1 range.

Public function RandomInt64(N: int64): int64;

A relatively slow procedure to get a 64 bit integer random number.

Public function RandomBoolean: boolean;

A simple Yes/No function that with 50% chance returns true or false. Something like flipping a coin...

Public function RandomSign: longint;

Randomly provides "-1", "0" or "1" with equal chances.

Public function Random32bit: LongWord;

Returns a random number in 1 .. High(LongWord) range.


Generated by PasDoc 0.16.0.