当前位置:网站首页>利用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;
}


边栏推荐
猜你喜欢
随机推荐
Flask framework learning: template inheritance
家·谱——人脸识别家谱系统
【win10+cuda7.5+cudnn6.0安装caffe③】编译及测试caffe
CentOS7静默安装Oracle11g_转载
0708作业---商品信息
[ARM] rk3399 mounts nfs error
flaks框架学习:在 URL 中添加变量
吃瓜教程task02 第3章 线性模型
QT Mat转HObject和HObject转Mat 图像视觉处理
原生态mongo连接查询代码
基于 TF-IDF 文本匹配实战详细教程 数据+代码 可直接运行
redis集群模式--解决redis单点故障
面试宝典一: code题目记录
一、Jmeter环境部署
实战noVNC全过程操作(包含遇到的问题和解决)
arraylist之与linkedlist
task06 PyTorch生态
(一)性能实时监控平台搭建(Grafana+Influxdb+Jmeter)
flaks framework learning: adding variables to the URL
imx6 yocto编译备忘









