当前位置:网站首页>A method of generating non repeated numbers in nodejs
A method of generating non repeated numbers in nodejs
2022-06-24 10:43:00 【easonxie】
background
Recently when I was doing the demand , There is a management interface that needs to pass an unsigned when calling 32 Bit shaping file ID, That is to say 0 ~ 4294967295 Number between , Every time the interface is called, this file ID Can't repeat .
One 、 Database solutions
The more serious thing to do is , Create a database table for the file , Set a self increasing Int Type primary key id, Then every time you synchronize a file , Insert a file into the database table of the file , To generate a file id, Put this id Pass it to the backstage . But it's more troublesome , After all, our goal is to have a non - Duplicate File id, Doing too much for this purpose is not worth the loss .
Two 、 Using time stamps
JS The timestamp of is converted into a number 13 position ,32 The maximum unsigned integer number of bits is 4294967295, There is no way to treat the timestamp as a file directly id To use . You can change your mind . adopt JS Of Data.now() Get the current timestamp , Subtract a specified timestamp , Calculate the direct time interval between the two , Then multiply this time interval by 100, And then add 0-99 The random number .
The number thus generated , Bits and tens are random numbers , Hundreds or more are self incrementing timestamps . It is almost impossible to repeat , If greater accuracy is required , You can put 100 Switch to 1000, And then add 0-999 The random number .
/**
* precision precision 100 1000 10000
*/
function getNumberUid(precision){
const rawPre = (Date.now() - new Date(1624206802955).getTime()) / precision;
const preNumber = Number(rawPre.toFixed()) * precision;
const randam = Math.floor(Math.random() * precision);
return preNumber + randam;
}3、 ... and 、 expand
The numbers generated in this way still have a very small probability of repetition , But it can basically handle the interface calls of the management side environment , Ensure no repetition . If it is the only one with a very large amount of data Id Generate , You still need to use a database or other methods . For example, you can refer to teacher liaoxuefeng's article 《 Distributed uniqueness ID generator 》
边栏推荐
- Niuke-top101-bm29
- Thread operation principle
- Practice sharing of packet capturing tool Charles
- Baidu online disk download has been in the process of requesting solutions
- Distribute proofs of manuscripts by scanning
- Quick completion guide for mechanical arm (zero): main contents and analysis methods of the guide
- SF Technology Smart logistics Campus Technology Challenge (June 19, 2022) [AK]
- 常用的第三方ui框架
- Quick completion guide for mechanical arm (I): development overview of mechanical arm
- 百度网盘下载一直请求中问题解决
猜你喜欢

Petit guide de construction rapide du bras mécanique (II): application du bras mécanique

26.删除有序数组的重复项

283.移动零

Sort out interface performance optimization skills and kill slow code

【JS逆向分享】某个网站社区信息

2022年智能机器人与系统国际研讨会(ISoIRS 2022)

Flink集群搭建以及企业级yarn集群搭建

Uniapp implements the function of clicking to make a call

Flink checkPoint和SavePoint

JMeter interface test tool foundation - badboy tool
随机推荐
JMeter interface test tool foundation - use badboy to record JMeter script
3. addition, deletion, modification and query of employees
Thread pool execution process
Shape change loader loads jsjs special effect code
Common methods of thread scheduling
Flink集群搭建以及企业级yarn集群搭建
Baidu online disk download has been in the process of requesting solutions
Illustration miscellaneous [for archiving to prevent loss]
JMeter interface test tool foundation - badboy tool
机械臂速成小指南(一):机械臂发展概况
6. package management business development
Web project deployment
Cool interactive animation JS special effects implemented by p5.js
Pycharm shortcut keys
Process and multithreading
cuda runtime error (801) : Raw out
26.删除有序数组的重复项
[IEEE publication] 2022 International Conference on industrial automation, robotics and Control Engineering (iarce 2022)
【IEEE出版】2022年工业自动化,机器人与控制工程国际会议(IARCE 2022)
JMeter接口测试工具基础— 使用Badboy录制JMeter脚本