Skip to content

rand.sys #

Constants #

const seed_len = 1

Implementation note:

C.rand returns a pseudorandom integer from 0 (inclusive) to C.RAND_MAX (exclusive) C.rand() is okay to use within its defined range. (See: https://web.archive.org/web/20180801210127/http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx) The problem is, this value varies with the libc implementation. On windows, for example, RAND_MAX is usually a measly 32767, whereas on (newer) linux it's generally 2147483647. The repetition period also varies wildly. In order to provide more entropy without altering the underlying algorithm too much, this implementation simply requests for more random bits until the necessary width for the integers is achieved.

struct SysRNG #

struct SysRNG {
	buffer.PRNGBuffer
mut:
	seed u32 = seed.time_seed_32()
}

SysRNG is the PRNG provided by default in the libc implementiation that V uses.

fn (SysRNG) seed #

fn (mut r SysRNG) seed(seed_data []u32)

r.seed() sets the seed of the accepting SysRNG to the given data.

fn (SysRNG) default_rand #

fn (r SysRNG) default_rand() int

r.default_rand() exposes the default behavior of the system's RNG (equivalent to calling C.rand()). Recommended for testing/comparison b/w V and other languages using libc and not for regular use. This is also a one-off feature of SysRNG, similar to the global seed situation. Other generators will not have this.

fn (SysRNG) u8 #

fn (mut r SysRNG) u8() u8

byte returns a uniformly distributed pseudorandom 8-bit unsigned positive byte.

fn (SysRNG) u16 #

fn (mut r SysRNG) u16() u16

u16 returns a uniformly distributed pseudorandom 16-bit unsigned positive u16.

fn (SysRNG) u32 #

fn (r SysRNG) u32() u32

u32 returns a uniformly distributed pseudorandom 32-bit unsigned positive u32.

fn (SysRNG) u64 #

fn (r SysRNG) u64() u64

u64 returns a uniformly distributed pseudorandom 64-bit unsigned positive u64.

fn (SysRNG) block_size #

fn (r SysRNG) block_size() int

block_size returns the number of bits that the RNG can produce in a single iteration.

fn (SysRNG) free #

unsafe
fn (mut rng SysRNG) free()

free should be called when the generator is no longer needed