lundi 28 mars 2022

Change random value in 2D array as efficiently as possible in JavaScript

var cells = [
    [1, 2, 3, 5],
    [1, 1, 2, 3],
    [1, 1, 1, 2],
    [0, 1, 0, 1]
]

The challange is to pick one equally random 0 from cells and change it randomly to a 1 or 2.

The chance of it changing to a 1 should be 90%. The chance of a 2 10%.

cells will always be a 4x4 array.

I am wondering if there are some faster solutions (even if it is uglier) that I cannot find. Anything slighly faster is welcome as an answer!

Your solution should rather not use external libraries.




Aucun commentaire:

Enregistrer un commentaire