美文网首页
获取随机数

获取随机数

作者: 小码农_影 | 来源:发表于2021-03-18 21:02 被阅读0次
 var bigInt = require("big-integer");

var Snowflake = /** @class */ (function() {
function Snowflake(_workerId, _dataCenterId, _sequence) {
    // this.twepoch = 1288834974657;
    this.twepoch = 0;
    this.workerIdBits = 5;
    this.dataCenterIdBits = 5;
    this.maxWrokerId = -1 ^ (-1 << this.workerIdBits); // 值为:31
    this.maxDataCenterId = -1 ^ (-1 << this.dataCenterIdBits); // 值为:31
    this.sequenceBits = 12;
    this.workerIdShift = this.sequenceBits; // 值为:12
    this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; // 值为:17
    this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; // 值为:22
    this.sequenceMask = -1 ^ (-1 << this.sequenceBits); // 值为:4095
    this.lastTimestamp = -1;
    //设置默认值,从环境变量取
    this.workerId = 1;
    this.dataCenterId = 1;
    this.sequence = 0;
    if (this.workerId > this.maxWrokerId || this.workerId < 0) {
        throw new Error('config.worker_id must max than 0 and small than maxWrokerId-[' + this.maxWrokerId + ']');
    }
    if (this.dataCenterId > this.maxDataCenterId || this.dataCenterId < 0) {
        throw new Error('config.data_center_id must max than 0 and small than maxDataCenterId-[' + this.maxDataCenterId + ']');
    }
    this.workerId = _workerId;
    this.dataCenterId = _dataCenterId;
    this.sequence = _sequence;
}
Snowflake.prototype.tilNextMillis = function(lastTimestamp) {
    var timestamp = this.timeGen();
    while (timestamp <= lastTimestamp) {
        timestamp = this.timeGen();
    }
    return timestamp;
};
Snowflake.prototype.timeGen = function() {
    return Date.now();
};
Snowflake.prototype.nextId = function() {
    var timestamp = this.timeGen();
    if (timestamp < this.lastTimestamp) {
        throw new Error('Clock moved backwards. Refusing to generate id for ' +
            (this.lastTimestamp - timestamp));
    }
    if (this.lastTimestamp === timestamp) {
        this.sequence = (this.sequence + 1) & this.sequenceMask;
        if (this.sequence === 0) {
            timestamp = this.tilNextMillis(this.lastTimestamp);
        }
    } else {
        this.sequence = 0;
    }
    this.lastTimestamp = timestamp;
    var shiftNum = (this.dataCenterId << this.dataCenterIdShift) |
        (this.workerId << this.workerIdShift) |
        this.sequence; // dataCenterId:1,workerId:1,sequence:0  shiftNum:135168
    var nfirst = new bigInt(String(timestamp - this.twepoch), 10);
    nfirst = nfirst.shiftLeft(this.timestampLeftShift);
    var nnextId = nfirst.or(new bigInt(String(shiftNum), 10)).toString(10);
    return nnextId;
};
return Snowflake;

 }());
var work_id=parseInt(Math.random()*1000)%31;
var datacenter_id=parseInt(Math.random()*1000)%31;
var temptHG_Snowflake = new Snowflake(work_id, datacenter_id, 0);
exports.HG_Snowflake=temptHG_Snowflake;

相关文章

  • swift 随机数

    获取 Int 类型的随机数 调用方法如下: 获取Float 类型的随机数 调用方法如下: 获取CGFloat 类型...

  • 获取随机数

    获取随机数的几个小栗子 1.颜色随机 2.获取两数之间的随机数 指定值中获取随机数 4.获取随机的ip地址(0-255)

  • [MySQL]随机数

    获取[0, 1)的随机数 select rand() 获取[i, j)的随机数 select floor(i + ...

  • c课堂笔记 day-5

    冒泡排序 2.获取随机数 //获取0~100之间的随机数 srand(time(NULL));//此语句不能放在循...

  • iOS彩票类排列组合算法

    1. 从一个范围内获取一组不重复的随机数,返回这个数组: 头文件: /*获取随机数@param count获取随机...

  • 三、常用类和方法

    1、随机数的获取方法 结果:

  • 获取随机数Math.random()

    获取0~7之间的随机数

  • Sass数字函数-random()

    用来获取一个随机数

  • rand.Seed(time.Now().UnixNano())

    rand.Int获取随机数,不加随机种子,每次遍历获取都是重复的一些随机数据 rand.Seed(time.Now...

  • js简易抽奖盘制作

    原理1、利用 Math.floor获取随机数字;2、点击按钮后,获取随机数字存为变量;3、设置循环定时器依次修改背...

网友评论

      本文标题:获取随机数

      本文链接:https://www.haomeiwen.com/subject/vwnpcltx.html