当前位置:网站首页>利用rand函数随机生成uuid
利用rand函数随机生成uuid
2022-08-11 05:16:00 【FussyCat】
- 利用rand()函数,产生的随机数,用来构造uuid。
- uuid格式为:
%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x - 特别说明:
必须要加上第17行的随机种子,才能产生随机数,否则,每次调用GenerateUuid(),获取到的uuid结果都是一样的值。
#include <string.h>
#include <string>
#include "time.h"
/* * 功能描述: 生成随机uuid * 参数描述: uuid字符串为36个字符加结束符,字符串长度为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;
}


边栏推荐
猜你喜欢

task06 PyTorch生态

selenuim使用cookie登录京东

玩转mysql之查看mysql版本号

(3) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Node_explorer+Jmeter)

vftpd本地可以连接,远程连接超时的解决

【win10+cuda7.5+cudnn6.0安装caffe⑥】报错及处理方式

The most complete installation tutorial of Pytorch (one step)

【翻译】博客游戏项目Q1K3 – 制作

【CSDN21天学习挑战赛】第一天,配置环境外加实现mnist手写数字识别

UML基本概念——动态视图
随机推荐
pytorch安装笔记——Pytorch在conda+CUDA10.2环境安装task01
[Verilog] I2S Master Test Bench
[Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
开炮,开炮
一、Jmeter环境部署
StarUML使用心得
redis分布式锁
批量修改数据库等视频文件名称
切分字符串进行输出显示
原生态mongo连接查询代码
UML基本概念——动态视图
吃瓜教程task03 第4章 决策树
Redis中RDB和AOF的区别
第4章 复合类型-2(指针)
Redis details
二、Jmeter 核心配置文件
flask框架学习:debug与配置项
【动态代理】CGLIB 动态代理的使用及原理
【win10+cuda7.5+cudnn6.0安装caffe④】安装pycaffe
Summary: Cross Validation