Is there a better way to populate this array in node.js?

1

Below is my function of node.js (I'm new to this), I would like to populate the array with random numbers (it can be zero as well), is there any faster way to do this? What am I doing?

QLearn.reset = function() {
       for (var a=0; a<100; a++) {
        for (var b=0; b<20; b++) {
            for (var c=0; c<100; c++){
                for (var d=0; d<QLearn.action; d++){
                    QL[a,b,c,d]=Math.random();
                }
            }
        }
    }
    
asked by anonymous 26.09.2016 / 15:29

1 answer

0

If you are in Node.js environment you can use modern JavaScript and do something like this:

function randomArray(){
    return [...Array(4)].map(Math.random);
}
const start = +new Date(); 
var QL =  [...Array(100)].map(randomArray);
const end = (+new Date()) - start;
console.log(QL, end); // 2ms

This code generates 100 subarrays of 4 random numbers each.

    
26.09.2016 / 22:28