Team:Lethbridge/randomize js
/* A 16-bit random number generator. This outputs an non-negative integer on the interval [0, n). This enjoys the property that it will never produce the same number twice in a row.
- /
function nonRedundantRandom(n) {
// Give this function a member that stores the last computed random // number. This number will not be be generated this time. If it is // generated, the function will recurse until it generates a different // number. if (typeof nonRedundantRandom.last === 'undefined') { nonRedundantRandom.last = -1; }
// Handle the case where n is less than 2. If n is 1, then the function // will perform infinte recursions, and if it is negative the behaviour // will be undefined. Return 0 and forgeet about these silly inputs :) if (n < 2) { return 0; }
// Generate some random 16-bit numbers based of of the largest unsigned // 16-bit prime var large_prime = 65521; var twist0 = Math.floor(Math.random() * 65521); var twist1 = Math.floor(Math.random() * 65521); var twist2 = Math.floor(Math.random() * 65521);
// Do a series of bit shifts, swaps, flips, and other goofy things twist0 = (twist1 >> (twist0 << Math.floor(Math.random() * 16))); twist0 = ((twist0 >> Math.floor(Math.random() * 16)) ^ twist2); twist1 = twist0 & twist2; twist2 = twist1 ^ (twist2 & ((twist0 >> 8) + (twist0 << 8)));
// This is a randome number on the interval [0, ] var random_number = Math.abs((twist0 * twist1) ^ twist2) % n; if (random_number != nonRedundantRandom.last) { nonRedundantRandom.last = random_number; return random_number; } else { return nonRedundantRandom(n); }
}
/* Returns a random color from its set
- /
function randomColor() {
var colors = [ '#fd6769', // red '#fec66a', // tangerine '#6ce980', // green '#898cd2', // blue '#9e68cc' // purple ]; var index = nonRedundantRandom(colors.length); return colors[index];
}
/* Returns a random texture from its set
- /
function randomTexture() {
var textures = [ 'url(../img/tiled_dots.png)', 'url(../img/tiled_holes.png)', 'url(../img/tiled_dlstripes.png)', 'url(../img/tiled_drstripes.png)', 'url(../img/tiled_hstripes.png)', 'url(../img/tiled_vstripes.png)', 'url(../img/tiled_diamond.png)', 'none' ]; var index = nonRedundantRandom(textures.length); return textures[index];
}