How to decide between two numbers randomly using javascript?

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

    Math.round(Math.random()) returns a 0 or a 1, each value just about half the time.

    You can use it like a true or false, 'heads' or 'tails', or as a 2 member array index-

    ['true','false'][Math.round(Math.random())] will return 'true' or 'false'...

提交回复
热议问题