猫史档案馆


随机数函数,再也不用担心用random搞不出来随机数了!)

用户:yee剑走天下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)
}

又氵了一个帖子))))


回复

上一页1 页 / 共 1下一页
囧仙_official囧仙_official

c++的rand没有啦~

qwq

点赞0


评论


盗码师盗码师

yeeee

点赞0


评论


盗码师盗码师

好东西()()(

点赞0


评论


浮生awa浮生awa

这样化简到一行岂不美哉,而且更有b格

还可以加个函数注释,显得更加nb(什

/**
 * @param {Number} min  随机生成数的最小值
 * @param {Number} max  随机生成数的最大值
 */
Math.randint = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)

硬是把简单的代码写成了萌新看不懂的样子(bushi

点赞0


评论


yee剑走天下yee剑走天下

究极繁化版()())()

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


评论


ฅ智慧编程猫ฅ----(和平工作室室长)ฅ智慧编程猫ฅ----(和平工作室室长)

你信不信我能做出不用Math的随机()()()

点赞0


评论


源码极客源码极客

yee~

点赞0


评论


一个STUB用户_14302596一个STUB用户_14302596

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


评论