Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_rand.h File Reference
#include "vl_numtypes.h"
#include "vl_memory.h"
+ Include dependency graph for vl_rand.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define VL_H_RANDOM_SPLIT(randPtr, resultPtr)   (*((vl_ularge_t*)(resultPtr)) = vlRandNext(randPtr))
 This macro distributes random bytes to the specified pointer.
 
#define vlRandFx4(randPtr, resultPtr)
 Generates four random floats.
 

Typedefs

typedef vl_ularge_t vl_rand
 Random state type.
 

Functions

vl_rand vlRandInit ()
 Initializes a random state (aka, seed) based on the current time.
 
vl_rand vlRandNext (vl_rand *rand)
 Returns the "next" 64-bit integer.
 
void vlRandFill (vl_rand *rand, void *mem, vl_ularge_t len)
 Fills the specified region of memory with random bytes.
 
vl_float32_t vlRandF (vl_rand *randPtr)
 Generate a random float.
 
void vlRandFx2 (vl_rand *randPtr, vl_float32_t *resultPtr)
 Generates two random floats.
 
vl_float64_t vlRandD (vl_rand *randPtr)
 Generate a random double.
 

Macro Definition Documentation

◆ VL_H_RANDOM_SPLIT

#define VL_H_RANDOM_SPLIT ( randPtr,
resultPtr )   (*((vl_ularge_t*)(resultPtr)) = vlRandNext(randPtr))

This macro distributes random bytes to the specified pointer.

This macro results in a reinterpretation suitable for all integer types, but not for floating point types.

The total number of bytes distributed is equal to sizeof(vl_ularge_t).

◆ vlRandFx4

#define vlRandFx4 ( randPtr,
resultPtr )
Value:
vlRandFx2(randPtr, (vl_float32_t*)(resultPtr));\
vlRandFx2(randPtr, ((vl_float32_t*)(resultPtr)) + 2)
VL_F32_T vl_float32_t
32-bit floating point number type.
Definition vl_numtypes.h:111
void vlRandFx2(vl_rand *randPtr, vl_float32_t *resultPtr)
Generates two random floats.
Definition vl_rand.c:126

Generates four random floats.

Range of returned floats is in range of 0...1.

Parameters
randPtrpointer to random state
resultPtrwhere the result will be stored.
Complexity of O(1) constant.
Returns
void

Typedef Documentation

◆ vl_rand

Random state type.

The PRNG algorithm used by the VL library is splitmix64 or splitmix32. It offers fast pseudo-random number generation and passes Big Crush with minimal state. All vlRand* functions are almost entirely branchless.

Function Documentation

◆ vlRandD()

vl_float64_t vlRandD ( vl_rand * randPtr)

Generate a random double.

Range of returned value is in 0...1.

Parameters
randPtrpointer to random state
Complexity of O(1) constant.
Returns
random double.

◆ vlRandF()

vl_float32_t vlRandF ( vl_rand * randPtr)

Generate a random float.

Range of returned value is in 0...1.

Parameters
randPtrpointer to random state
Complexity of O(1) constant.
Returns
random float
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vlRandFill()

void vlRandFill ( vl_rand * rand,
void * mem,
vl_ularge_t len )

Fills the specified region of memory with random bytes.

This function operates on blocks of 8 bytes at a time, until it fills in the last few remaining bytes.

Parameters
randrand state
memdestination
lentotal number of bytes to fill.
Complexity of O(n) linear.

◆ vlRandFx2()

void vlRandFx2 ( vl_rand * randPtr,
vl_float32_t * resultPtr )

Generates two random floats.

Range of returned floats is in range of 0...1.

Parameters
randPtrpointer to random state
resultPtrwhere the result will be stored
Complexity of O(1) constant.
+ Here is the call graph for this function:

◆ vlRandInit()

vl_rand vlRandInit ( )

Initializes a random state (aka, seed) based on the current time.

This function can be skipped entirely, assuming another seed integer can be provided.

Complexity of O(1) constant.
Returns
random state/seed.
+ Here is the call graph for this function:

◆ vlRandNext()

vl_rand vlRandNext ( vl_rand * rand)

Returns the "next" 64-bit integer.

This function has a side effect of updating the random state.

Complexity of O(1) constant.
Parameters
randpointer to random state
Returns
unsigned 64-bit integer
+ Here is the caller graph for this function: