mirror of
https://github.com/soconnor0919/eceg431.git
synced 2025-12-12 07:04:43 -05:00
55 lines
930 B
Plaintext
55 lines
930 B
Plaintext
/*
|
|
Random Number Generator
|
|
Original author: Taylor Wacker
|
|
Modified by: Connor McKay, Sean O'Connor
|
|
https://gist.github.com/greneholt/2212294
|
|
|
|
This is a pseudo random number generator that uses the
|
|
Linear Congruential Generator (LCG) to generate random
|
|
numbers.
|
|
*/
|
|
class Random {
|
|
static int x;
|
|
|
|
/*
|
|
Sets a new seed value.
|
|
*/
|
|
function void seed(int seed) {
|
|
let x = seed;
|
|
return;
|
|
}
|
|
|
|
/*
|
|
Returns a mod b. b must be positive.
|
|
*/
|
|
function int mod(int a, int b) {
|
|
if (a < 0) {
|
|
let a = -a;
|
|
}
|
|
|
|
while ((a + 1) > b) {
|
|
let a = a - b;
|
|
}
|
|
|
|
return a;
|
|
}
|
|
|
|
/*
|
|
Returns the next random number. Can be negative or positive.
|
|
*/
|
|
function int next() {
|
|
let x = 7919 + (17*x);
|
|
return x;
|
|
}
|
|
|
|
/*
|
|
Returns a random value between x (inclusive) and y (non-inclusive).
|
|
y must be greater than x.
|
|
*/
|
|
function int between(int x, int y) {
|
|
var int diff;
|
|
let diff = y - x;
|
|
return Random.mod(Random.next(), diff) + x;
|
|
}
|
|
}
|