I would like to create an array of n (steps
) integers within an interval [a (min
), b (max
)] which can be done like this (there are probably smarter ways):
function randomFromInterval(min, max, steps) {
return new Array(steps).fill(0).map(n => Math.floor(Math.random() * (max - min + 1) + min));
}
console.log(randomFromInterval(1, 100, 10))
Unfortunately, with truly random (yeah I know) integers, it could happen that the result of randomFromInterval(1, 100, 5)
is for example [1,2,3,4,5]
which I would like to mitigate. The values of randomFromIntervalButSpread(min, max, steps)
should therefore be spread out over the interval in such a way that
randomFromIntervalButSpread(0, 4, 5) => [0,1,2,3,4]
randomFromIntervalButSpread(10, 60, 5) => [1X,2X,3X,4X,5X] // X being 0-9
.
.
.
To summarize: The numbers should be random in the sense that if steps > |[min, max]| the results differ from iteration to iteration but are never grouped together within the interval.
Aucun commentaire:
Enregistrer un commentaire