Fill a "2D" Array with random values
-
I got a problem with arrays in js and I'm starting to lose it. Every iteration generates a different value. However, it appears that it pushes the same value in the entire row; so the entire row has the same "random" value every time. I want to have every 'x' to have a "random" value.
for (var x = 0; x <= 90; x++) {
var col = CustomRandom();
memBlock[x, 68] = Math.floor(col.next() * 255);
}The "2D" Array is created with a recursive function I found on the web:
function createMemblock(length) {
var a = new Array(length || 0);
if (arguments.length > 1) {
var args = Array.prototype.slice(arguments, 1);
for (var i = 0; i < length; i++) {
a[i] = createMemblock.apply(this, args);
}
}
return a;
}And here is the entire script, just in case:
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();function createMemblock(length) { var a = new Array(length || 0); if (arguments.length > 1) { var args = Array.prototype.slice(arguments, 1); for (var i = 0; i < length; i++) { a\[i\] = createMemblock.apply(this, args); } } return a; } var CustomRandom = function (nseed) { var seed, constant = Math.pow(2, 13) + 1, prime = 1987, maximum = 1000; if (nseed) { seed = nseed; } if (seed == null) { seed = (new Date()).getTime(); } return { next: function (min, max) { seed \*= constant; seed += prime; return min && max ? min + seed % maximum / maximum \* (max - min) : seed % maximum / maximum; } } }
-
I got a problem with arrays in js and I'm starting to lose it. Every iteration generates a different value. However, it appears that it pushes the same value in the entire row; so the entire row has the same "random" value every time. I want to have every 'x' to have a "random" value.
for (var x = 0; x <= 90; x++) {
var col = CustomRandom();
memBlock[x, 68] = Math.floor(col.next() * 255);
}The "2D" Array is created with a recursive function I found on the web:
function createMemblock(length) {
var a = new Array(length || 0);
if (arguments.length > 1) {
var args = Array.prototype.slice(arguments, 1);
for (var i = 0; i < length; i++) {
a[i] = createMemblock.apply(this, args);
}
}
return a;
}And here is the entire script, just in case:
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();function createMemblock(length) { var a = new Array(length || 0); if (arguments.length > 1) { var args = Array.prototype.slice(arguments, 1); for (var i = 0; i < length; i++) { a\[i\] = createMemblock.apply(this, args); } } return a; } var CustomRandom = function (nseed) { var seed, constant = Math.pow(2, 13) + 1, prime = 1987, maximum = 1000; if (nseed) { seed = nseed; } if (seed == null) { seed = (new Date()).getTime(); } return { next: function (min, max) { seed \*= constant; seed += prime; return min && max ? min + seed % maximum / maximum \* (max - min) : seed % maximum / maximum; } } }
It's not so tricky to create a 2d array, you just need to go about it a little differently to some other langs. First, you want to create the first dimension. Next, you need to fill each element in this dimension with a second array. In my case, I've abstracted a screen. The first dimension is the rows, or y-coord. Each of these rows contains an array that holds all of the pixels in that line - the columns. This code simply creates a 320x200 array and fills it with a simple xor pattern. Of course, the other way to do it is to do what the compiler does - simply allocate a block of memory and then index into it yourself. The imageData object of the canvas does this - it has a 1d array that represents the pixels. So (y*width*4)+(x*4) will give you the index of the pixel you want. Since it's a 32bit pixel, there's 4 bytes for each one. It saves indexing and is much quicker and easy enough enough. That said, here's the code I promised earlier.
var screenArray;
function mInit()
{
var x, y, width=320, height=200;
var result = new Array;for (y=0; y<height; y++) { result\[y\] = new Array; for (x=0; x<width; x++) { result\[y\]\[x\] = x^y; } } screenArray = result; console.log(screenArray);
}
Make it work. Then do it better - Andrei Straut