Creating array of length n with random numbers in JavaScript

后端 未结 6 1707
耶瑟儿~
耶瑟儿~ 2020-12-09 03:44

Following up on this answer for creating an array of specified length, I executed the below to get a corresponding result but filled with random numbers, instead of zeros.

6条回答
  •  甜味超标
    2020-12-09 04:28

    Short and simple ES6 approach -

    // randomly generated n = 4 length array 0 <= array[n] <= 9
    var randoms = Array.from({length: 4}, () => Math.floor(Math.random() * 10));
    

    Enjoy!

提交回复
热议问题