Creating array of length n with random numbers in JavaScript

后端 未结 6 1695
耶瑟儿~
耶瑟儿~ 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:37

    `const t = Array.from({length: n}, mapArrowFx);

    1) const t10 = Array.from({length: 10}, (v, k) => k); [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    2) const tEven = Array.from({length: 10}, (v, k) => 2*k); [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

    ........

    3)

    let n=100; const tRandom= Array.from({length: n}, (v, k) => Math.floor(Math.random()*n));

    ...

提交回复
热议问题