当前位置:网站首页>Randomly generate uuid using rand function
Randomly generate uuid using rand function
2022-08-11 05:46:00 【FussyCat】
- 利用rand()函数,产生的随机数,用来构造uuid.
- uuid格式为:
%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x
- 特别说明:
must be added17random seed for row,to generate random numbers,否则,每次调用GenerateUuid(),获取到的uuidThe results are all the same value.
#include <string.h>
#include <string>
#include "time.h"
/* * 功能描述: 生成随机uuid * 参数描述: uuid字符串为36characters plus a terminator,字符串长度为37 * * uuid 长度: 8 - 4 - 4 - 4 - 12 * uuid 格式:"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x" * 字符 序号: 0~3 3 4~5 5 6~7 7 8~9 9 10~15 */
int generate_uuid(char buf[37])
{
const char *s = "89ab";
char *p = buf;
int pos;
srand((unsigned int) time(NULL)); // 加入随机种子
for(pos = 0; pos < 16; pos++) {
int randNum = rand();
int b = randNum % 255;
switch(pos) {
case 6:
sprintf(p, "4%x", b % 15);
break;
case 8:
sprintf(p, "%c%x", s[rand() % strlen(s)], b % 15);
break;
default:
sprintf(p, "%02x", b);
break;
}
p += 2;
switch(pos) {
case 3:
case 5:
case 7:
case 9:
*p++ = '-';
break;
}
}
*p = 0;
fprintf(stdout, "uuid: %s", buf);
return 0;
}
边栏推荐
猜你喜欢
RK3399上的Tengine实践笔记
并发编程之线程基础
(1) Construction of a real-time performance monitoring platform (Grafana+Influxdb+Jmeter)
(2) Docker installs Redis in practice (persistent AOF and RDB snapshots)
Flask框架学习:路由的尾部斜杠
(二)性能实时监控平台搭建(Grafana+Prometheus+Jmeter)
【win10+cuda7.5+cudnn6.0安装caffe⑥】报错及处理方式
(3) How Redis performs stress testing
搭建PX4开发环境
【分享】一个免费语料库
随机推荐
(3) How Redis performs stress testing
CSDN 社区内容创作规范
arraylist之与linkedlist
for循环使用多线程优化
PyTorch显存机制分析
我的四核Cortex-A53学习之路
简单做份西红柿炒蛋778
【转载】CMake 语法 - 详解 CMakeLists.txt
数组的用法
imx6 yocto编译备忘
Flask framework learning: template inheritance
postman脚本的应用
RK3399上的Tengine实践笔记
Keras与tensorflow 使用基础
(一)Docker安装Redis实战(一主二从三哨兵)
第10章 对象和类 -1
Minecraft
第8章 函数探幽-2
pytorch安装笔记——Pytorch在conda+CUDA10.2环境安装task01
信息学奥赛