Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Fill a "2D" Array with random values

Fill a "2D" Array with random values

Scheduled Pinned Locked Moved JavaScript
javascriptdata-structurestoolshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    0bx
    wrote on last edited by
    #1

    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;
    
                    }
                }
            }
    
    E 1 Reply Last reply
    0
    • 0 0bx

      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;
      
                      }
                  }
              }
      
      E Offline
      E Offline
      enhzflep
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups