How to decide between two numbers randomly using javascript?

前端 未结 4 414
野性不改
野性不改 2020-12-28 12:00

I want a javascript script that choose either value1 or value2 randomly, not between the two values , just the actual values.

Thanks!!!!

4条回答
  •  北海茫月
    2020-12-28 12:37

    The Math.random[MDN] function chooses a random value in the interval [0, 1). You can take advantage of this to choose a value randomly.

    var chosenValue = Math.random() < 0.5 ? value1 : value2;
    

提交回复
热议问题