Skip to content

Instantly share code, notes, and snippets.

@greneholt
Created March 27, 2012 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greneholt/2212294 to your computer and use it in GitHub Desktop.
Save greneholt/2212294 to your computer and use it in GitHub Desktop.
Jack pseudorandom number generator
/*
Random Number Generator
Original author: Taylor Wacker
Modified by: Connor McKay
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment