可恶的Math.random()

生成随机数1-10   (包含1和10)

结果是这样的:Math.floor(Math.random()*10+1)  那么问题又来了

Math.floor(Math.random()*10)生成的只是0-9的整数

生成0-10 怎么生成?

Math.floor(Math.random()*11)   啊啊啊啊啊.....然鹅我想了很久

不要把问题复杂化了,只是简单的数字游戏

不想要0   Math.random()*10+1           相当于(0,10] [1,10]

0和上限都不想要   Math.random()*(10-1)+1     相当于(0,10) [1,9]

不想要上限  Math.random()*10            相当于[0,10) [0,9]

想要0和上限   Math.random()*(10+1)          相当于[0,10] [0,11)

posted on 2017-05-25 16:57  编程是个无底洞  阅读(899)  评论(0编辑  收藏  举报

导航