dimanche 29 novembre 2020

How can I generate an array of random numbers that fluctuate by 1 in javascript?

I want to be able to generate the following array (or something like it) in javascript:

[
  52, // random number 0-100
  53, // random +1 or -1
  54, // random +1 or -1
  53, // random +1 or -1
  52, // random +1 or -1
  53, // random +1 or -1
  52, // random +1 or -1
  51, // random +1 or -1
  50, // random +1 or -1
  51, // random +1 or -1
  // etc., etc., etc.
]

How can I do that?

I've tried this, but I always get a random number followed by 1's and -1's only:

Array(50).fill(0).map((v, i, a) => i !== 0 ? (Math.round(Math.random()) ? a[i-1] + 1 : a[i-1] - 1) : Math.floor(Math.random() * 101))



Aucun commentaire:

Enregistrer un commentaire