今天遇到一个有意思的需求是要往数据库里插入某个范围内的随机数字模拟用户数据,命令很简单:
1 | db.mycollection.find( |
简单解释一下Math.ceil(Math.random()*10 + 20)}
,注意开区间和闭区间:Math.random()
值的范围: [0.0, 1.0)Math.random()*10
值的范围: [0.0, 10.0)Math.random()*10 + 20
值的范围: [20.0, 30.0)Math.ceil(Math.random()*10 + 20)}
值的范围: [20, 30]