7 47144679
8 49003259
9 44562068
10 42014734
由此可见,用随机文件生成的随机数,基本是全随机的,且通用于 CentOS、Ubuntu、MacOS
4. 使用 linux uuid (CentOS、Ubuntu支持,MacOS不支持)
UUID(Universally Unique Identifier,通用唯一识别码),格式包含32个16进制数字,以'-'连接号分为5段。
格式为 8-4-4-4-12 的32个字符,例如: 07e73165-1196-4194-98bb-a3bf7c96e34a
# cat /proc/sys/kernel/random/uuid 07e73165-1196-4194-98bb-a3bf7c96e34a
UUID 数量,理论上的总数为216 x 8=2128,约等于3.4 x 1038。 也就是说若每奈秒产生1兆个UUID,要花100亿年才会将所有UUID用完。
UUID 目的,是让分布式系统中的所有元素,都能有唯一的辨识信息,而不需要通过中央控制端来做辨识信息的指定。如此一来,每个人都可以创建不与其它人冲突的 UUID。在这样的情况下,就不需考虑数据库创建时的名称重复问题。它会让网络任何一台计算机所生成的uuid码,都是互联网整个服务器网络中唯一的。它的原信息会加入硬件,时间,机器当前运行信息等等。
UUID 格式:包含32个16进位数字,以“-”连接号分为五段,形式为8-4-4-4-12的32个字符。范例;550e8400-e29b-41d4-a716-446655440000 ,所以:
与 uuid类似的还有一个guid(全局唯一标识符)码,它由微软支持,它们由操作系统内核产生。
示例:使用 linux uuid 生成 40,000,000~50,000,000 之间的随机数
#!/bin/bash
# mimvp.com 2016.05.10
## Linux uuid
function mimvp_randnum_uuid() {
min=$1
max=$2
mid=$(($max-$min+1))
num=$(head -n 20 /proc/sys/kernel/random/uuid | cksum | cut -f1 -d ' ')
randnum=$(($num%$mid+$min))
echo $randnum
}
function print_randnum_uuid() {
for i in {1..10};
do
randnum=$(mimvp_randnum_uuid 40000000 50000000)
echo -e "$i t $randnum"
done
}
print_randnum_uuid
运行结果:
# sh mimvp_shell_rand.sh
1 44736535
2 43538760
3 40133914
4 41016814
5 49148972
6 40179476
7 48147712
8 45665645
9 40522150
10 44361996
5. 使用 openssl rand (CentOS、Ubuntu支持、MacOS 都支持,需安装 openssl,推荐)
openssl rand 用于产生指定长度个bytes的随机字符
# openssl rand --help Usage: rand [options] num where options are -out file - write to file -engine e - use engine e, possibly a hardware device. -rand file:file:... - seed PRNG from files -base64 - base64 encode output -hex - hex encode output










