js 產生亂數的方式

參考: http://liaosankai.pixnet.net/blog/post/19382531-%E4%BA%82%E6%95%B8-javascript-math.random()

如果想產生一個不是1就是2, 機率各半的數字
寫法是: parseInt(Math.random()*2)+1

Math.random()  --> (0,1)
Math.random() * 2 --> (0,2)
parseInt(Math.random() * 2 ) --> {0,1}
parseInt(Math.random() *2 ) + 1 --> {1,2}, and this is our request!

留言

熱門文章