mirror of
https://github.com/soconnor0919/eceg431.git
synced 2025-12-11 22:54:43 -05:00
project09 - snake game functional!
This commit is contained in:
54
09/Snake/Random.jack
Normal file
54
09/Snake/Random.jack
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user