Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
robot527
DevLogs
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
DevLogs
/
shell_scripts
/
random_num.sh
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
51 lines (41 loc) · 1.83 KB
master
Breadcrumbs
DevLogs
/
shell_scripts
/
random_num.sh
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
51 lines (41 loc) · 1.83 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /bin/bash
#1.使用系统的 $RANDOM 变量, 范围[0, 32767]
echo "system var RANDOM is" $RANDOM
#生成指定区间的随机整数
function rand_integer(){
min=$1
max=$(($2-$min+1))
num=$(($RANDOM+1000000000)) #增加一个10位的数再求余
echo $(($num%$max+$min))
}
if [ $# == 2 ];then
randi=$(rand_integer $1 $2)
else
randi=$(rand_integer 0 100)
fi
echo $randi
echo
#2.使用 date(system date and time)
echo "use date:"
date +%s
#获得时间戳,当前时间到:1970-01-01 00:00:00 相隔的秒数。
#如果用它做随机数,相同一秒的数据是一样的。在做循环处理,多线程里面就不能满足要求了。
date +%N #获得当前时间的纳秒数据,nanoseconds (000000000..999999999)。
#同一秒里面,大量循环也很难出现相同结果,不过不同时间里面还会有大量重复碰撞。
date +%s%N #这个可以说比较完美了,时间戳上加了纳秒。
echo
#3.使用/dev/random 和 /dev/urandom
# /dev/random 存储着系统当前运行环境的实时数据,是阻塞的随机数发生器,读取有时需要等待。
# /dev/urandom 非阻塞随机数发生器,读取操作不会产生阻塞。
echo "use /dev/urandom:"
head -20 /dev/urandom | cksum | cut -f 1 -d " "
echo
#4.读取 linux 的 uuid 码
#UUID码全称是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OSF) 的组织应用在分布式计算环境 (Distributed Computing Environment, DCE) 领域的一部分。
#它的格式包含32个16进制数字,以'-'连接号分为5段。形式为8-4-4-4-12 的32个字符。
echo "use UUID:"
cat /proc/sys/kernel/random/uuid | cksum | cut -f 1 -d " "
echo
#5. 使用 openssl rand
echo "use openssl rand:"
openssl rand 32 -base64 | cksum | cut -f 1 -d " "
You can’t perform that action at this time.