xxxxxxxxxx
56
// ---
// your mission: change the definition of the myRandom() function
// to return a random number---without using random() or
// Math.random(). The random number should be between 0 and 1.
//
// this function will be called to determine the brightness of
// pixels on the screen (continually displayed in order from
// left to right, top to bottom, several pixels per frame.)
//
// define other variables if you need to.
var state = 1;
function myRandom() {
// adapted from http://excamera.com/sphinx/article-xorshift.html
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
// javascript numbers overflow as negative, so map output to modulo
// plus 0.5
var ret = ((state % 100) / 200) + 0.5;
return ret;
}
// ---
// try to keep the code below unchanged (unless you have a really
// clever idea).
// ---
var pos = 0;
var step = 8;
var randomCanvas;
function setup() {
pixelDensity(1);
createCanvas(200, 200);
randomCanvas = createGraphics(40, 40);
}
function draw() {
background(0);
noSmooth();
randomCanvas.loadPixels();
for (var i = 0; i < step; i++) {
var pxval = myRandom() * 255;
for (var j = 0; j < 4; j++) {
randomCanvas.pixels[pos+j] = pxval;
}
pos += 4;
}
if (pos > randomCanvas.width * randomCanvas.height * 4) {
pos = 0;
}
randomCanvas.updatePixels();
scale(5);
image(randomCanvas, 0, 0);
}