Home
Sign Up
Sign In
V2EX
›
问与答
如何创建 M 行 N 位不重复随机数?
lin
·
Jan 16, 2015
· 3155 views
This topic created in 4308 days ago, the information mentioned may be changed or developed.
假定:
M = 10000
N = 7
最好能保存成txt。
注明语言。
其中我特别想知道shell的写法。
Supplement 1 ·
Jan 17, 2015
如果 N = 20 呢?
随机数
假定
写法
10 replies
•
2015-01-18 02:43:30 +08:00
1
a2z
Jan 16, 2015
http://www.cyberciti.biz/faq/bash-shell-script-generating-random-numbers/
2
wheatcc
Jan 16, 2015
1
生成1000000—1010000
然后打乱随机输出?
PS:非程序员,求轻拍。
3
tabris17
Jan 16, 2015
shuffle算法
4
582033
Jan 16, 2015
mkpasswd:
#!/usr/bin/env bash
m=10
n=20
for a in $(seq 1 $m)
do
mkpasswd -l $n
done
5
582033
Jan 16, 2015
https://gist.github.com/582033/49d33c6821259e4122af
6
luikore
Jan 16, 2015
1
ruby -e 'puts [*1..(1e7)].sample 10000'
7
lin
OP
Jan 16, 2015
@
luikore
请问这个方法能保证不重复吗?
8
luikore
Jan 16, 2015
@
lin
可以, sample 就是为了不重复设计的
9
lin
OP
Jan 17, 2015
@
luikore
如果 N = 20 呢?
我发现当“1e7”改成“1e9”,哪怕只是执行10行,都卡住没反应了。
10
luikore
Jan 18, 2015
@
lin
是的, 其实要先生成一个 1e9 那么大的数组...
直白的方法:
nums = {}
while nums.size < 10000
nums[(rand 1..(1e20.to_i))] = true
end
puts nums.keys
About
·
Help
·
Advertise
·
Blog
·
API
·
FAQ
·
Privacy
·
Solana
·
1397 Online
Highest 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 36ms ·
UTC 18:22
·
PVG 02:22
·
LAX 10:22
·
JFK 13:22
♥ Do have faith in what you're doing.
❯