本帖最后由 墨凌羽 于 2015-9-4 12:48 编辑
喵呜喵5 发表于 2015-9-4 10:17
直接rand呗
@楼主 3楼错哪里了来来来我们谈谈
一下是将3楼代码运行1000次后的输出结果。我对生成的1~8的每个数字出现的次数做了统计,结果如下
统计:
1出现次数:7933
2出现次数:8010
3出现次数:8070
4出现次数:8010
5出现次数:8041
6出现次数:7939
7出现次数:7813
8出现次数:8184
可以看出每个数字出现的次数很均匀。完全符合随机的要求
虽然说8个数字为同一个的概率只有(1/8)^8这么小 但偏偏被你撞上也不能怪谁。俗话说点背不能怪社会不是?就这样认为3楼脚本有问题是不是太过不厚道?
竟然说我家喵呜喵5的脚本有问题 分分钟打脸!
测试结果文件:
test.rar
(288.15 KB, 下载次数: 37)
【将后缀改成txt再打开】
测试代码File.open("test.txt","w") do |f| table = [] #随机1000*8次 1000.times do |x| list = [] for i in 1..8 list.push(rand(8) + 1) end f.write("第#{x+1}次:#{list}\n") table.push(list) end #统计各个数字出现次数 count = [0,0,0,0,0,0,0,0,0] table.each do |i| i.each do |n| case n when 1 count[1] += 1 when 2 count[2] += 1 when 3 count[3] += 1 when 4 count[4] += 1 when 5 count[5] += 1 when 6 count[6] += 1 when 7 count[7] += 1 when 8 count[8] += 1 end end count[0] += 8 end p count[0],count[1]/count[0] f.write("统计:\n") f.write("1出现次数:#{count[1]}\n") f.write("2出现次数:#{count[2]}\n") f.write("3出现次数:#{count[3]}\n") f.write("4出现次数:#{count[4]}\n") f.write("5出现次数:#{count[5]}\n") f.write("6出现次数:#{count[6]}\n") f.write("7出现次数:#{count[7]}\n") f.write("8出现次数:#{count[8]}\n") end
File.open("test.txt","w") do |f|
table = []
#随机1000*8次
1000.times do |x|
list = []
for i in 1..8
list.push(rand(8) + 1)
end
f.write("第#{x+1}次:#{list}\n")
table.push(list)
end
#统计各个数字出现次数
count = [0,0,0,0,0,0,0,0,0]
table.each do |i|
i.each do |n|
case n
when 1
count[1] += 1
when 2
count[2] += 1
when 3
count[3] += 1
when 4
count[4] += 1
when 5
count[5] += 1
when 6
count[6] += 1
when 7
count[7] += 1
when 8
count[8] += 1
end
end
count[0] += 8
end
p count[0],count[1]/count[0]
f.write("统计:\n")
f.write("1出现次数:#{count[1]}\n")
f.write("2出现次数:#{count[2]}\n")
f.write("3出现次数:#{count[3]}\n")
f.write("4出现次数:#{count[4]}\n")
f.write("5出现次数:#{count[5]}\n")
f.write("6出现次数:#{count[6]}\n")
f.write("7出现次数:#{count[7]}\n")
f.write("8出现次数:#{count[8]}\n")
end
|