用户:yee剑走天下查看:10 回复:8 评论:10 创建时间:2022-11-10T14:28:13
python里面()
有random.randint(好像是这样拼?)
但是JS里面,只给了你一个Math.random()
然后我就花了20秒写了个ljljljljljlj不能再lj的随机数函数()(
Math.randint = function(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
又氵了一个帖子))))
浮生awa这样化简到一行岂不美哉,而且更有b格
还可以加个函数注释,显得更加nb(什
/**
* @param {Number} min 随机生成数的最小值
* @param {Number} max 随机生成数的最大值
*/
Math.randint = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
硬是把简单的代码写成了萌新看不懂的样子(bushi
点赞0
评论
究极繁化版()())()
console.clear()
class random {
constructor(min, max) {
this.min = min;
this.max = max;
this.PI = Math.PI
this.poor = () => this.max > this.min ? ((this.max - this.min) * Math.PI / ((this.PI / Math.PI) * this.PI)) | 0 : ((this.min - this.max) * Math.PI / ((this.PI / Math.PI) * this.PI)) | 0
}
randint() {
return Math.floor(
Math.random() * (this.poor() + this.min)
)
}
};
class Random extends random {
constructor(min, max) {
super(min, max);
this.log = console.log;
}
randint() {
return super.randint()
}
};
globalThis.Random = Random;
var test = new Random(10, 100).randint();
console.log(test)点赞1
评论
var randomNum = (function(){
var today = new Date();
var seed = today.getTime();
function rnd(){
seed = ( seed * 9301 + 49297 ) % 233280;
return seed / ( 233280.0 );
};
return function rand(number){
return Math.ceil(rnd(seed) * number);
};
})();
线性同源生成器随机数,不用random(其实是把random的底层逻辑弄出来了)使用方法:
console.log(randomNum(100))点赞0
评论