How to decide between two numbers randomly using javascript?

前端 未结 4 413
野性不改
野性不改 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:28

    ~~(Math.random()*2) ? true : false
    

    This returns either 0 or 1. "~~" is a double bitwise NOT operator. Basically strips the the decimal part. Useful sometimes.

    It is supposed to be faster then Math.floor()

    Not sure how fast it is as a whole. I submitted it just for curiosity :)

提交回复
热议问题