为了账号安全,请及时绑定邮箱和手机立即绑定

关于underscore _.random 负数取随机数的问题?

关于underscore _.random 负数取随机数的问题?

Smart猫小萌 2018-09-11 13:26:59
1.underscore里面有一个方法_.random,定义如下:// Return a random integer between min and max (inclusive). // integer : {-1,0,1} 负整数 、 0 、正整数 // https://zh.wikipedia.org/wiki/%E6%95%B4%E6%95%B0 // [x,y]包括y,注意与Math.random的区别_.random = function(min, max) {    if (max == null) {        max = min;        min = 0;     }    return min + Math.floor(Math.random() * (max - min + 1)); };再传入正整数的时候没有问题:带入10,(max=10,min=0)return 0 + Math.floor(Math.random()*(10-0)+1); return 0 + Math.floor( [0,1)*11 ); return 0 + Math.floor( [0,11) );     return 0 + [0,10];     return [0,10]会生成一个0-10包括0,10之间的整数但是如果传入一个-10(max = -10,min = 0):return 0 + Math.floor(Math.random()*(-10-0)+1); return 0 + Math.floor( [0,1)* -9 ); return 0 + Math.floor( [0,-9) );     return 0 + [0,-9];     return [0,-9]这样只会生成0,-9包括0,-9之间的整数,没有-10。这样的话:Return a random integer between min and max (inclusive) 返回一个min到max(包括)之间的一个随机整数上面这句话就有问题了?负数的时候,无法生成最小的边界值。不知道我这样理解对不对,望指正
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 897 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号