Skip to content

Random.as

Drake Denston edited this page Sep 25, 2017 · 2 revisions

Information about Random.as

First Created: September 24th, 2017
Last Updated: September 25th, 2017
Current Status: Stable, yet to add more functions.
Works with the 5645 (v5.14) Build of Sven Co-Op.

Copy 'Random.as' to 'svencoop\scripts\plugins'

Then near the top of your plugin, type #include "Random";, then instantiate an object of one of the algorithms this library provides. For example:

#include "Random";

Random::PCG rng_obj = Random::PCG()

uint random_int = rng_obj.nextInt();
double random_double = rng_obj.nextDouble();

Documentation

This library lets you do Pseudo-Random Number Generation (PRNG) with different algorithms. Currently the algorithms you can use are PCG, Xorshift, Mersenne Twister.

Statistics

I've done some testing with the above listed algorithms and Math.RandomLong()'s generation. I made another plugin, that when supplying the correct console command and the algorithm you want, it will generate 100 numbers from 0-3 (inclusive both ends). From there I copied the results and used a tool to automatically tally each number. I ran this test 3 times with each algorithm. The results were as shown:

PCG()
0 - 22, 30, 20
1 - 22, 25, 20
2 - 25, 19, 30
3 - 31, 26, 30

Xorshift()
0 - 23, 19, 25
1 - 32, 28, 25
2 - 26, 28, 29
3 - 19, 25, 21

MersenneTwister()
0 - 16, 32, 28
1 - 23, 17, 22
2 - 33, 26, 20
3 - 28, 25, 30

Math.RandomLong()
0 - 18, 26, 30
1 - 25, 18, 21
2 - 28, 32, 26
3 - 29, 24, 23

Library Info

PCG

Constructor

//Input a uint64 greater than 0 to provide a seed
PCG(uint64 in_seed)

//Else, it will auto generate a seed from the current Unix Time
PCG()

Functions

//Return a double [0,1) (Inclusive, Exclusive)
double nextDouble()

//Return a random uint32 number
uint nextInt()

//Return a random uint32 [0,upper) (Inclusive, Exclusive)
uint nextInt(uint upper)

Xorshift

Constructor

//Input a uint64 greater than 0 to provide a seed
Xorshift(uint64 in_seed)

//Else, it will auto generate a seed from the current Unix Time
Xorshift()

Functions

//Return a double [0,1) (Inclusive, Exclusive)
double nextDouble()

//Return a random uint32 number
uint nextInt()

//Return a random uint32 [0,upper) (Inclusive, Exclusive)
uint nextInt(uint upper)

Mersenne Twister

Constructor

//Input a uint64 greater than 0 to provide a seed
MersenneTwister(uint64 in_seed)

//Else, it will auto generate a seed from the current Unix Time
MersenneTwister()

Functions

//Return a double [0,1) (Inclusive, Exclusive)
double nextDouble()

//Return a random uint32 number
uint nextInt()

//Return a random uint32 [0,upper) (Inclusive, Exclusive)
uint nextInt(uint upper)