这是我从0-32打印100个随机数的代码。现在我想按频率对接收到的整数进行排序。实现这一目标的最快方法是什么?
math.randomseed(os.time()) -- random initialize
math.random(); math.random(); math.random() -- warming up
for x = 1, 100 do
-- random generating
value = math.random(0,32)
print(value)
end
所需输出的示例如下所示
Output:
0:10
1:5
2:4
3:7
etc.
更简单的是做直方图,即由值索引的表。每当遇到值时,histogram [value]都会递增
histogram={}
for i = 0, 32 do
histogram[i]=0
end
math.randomseed(os.time()) -- random initialize
math.random(); math.random(); math.random() -- warming up
for x = 1, 100 do
-- random generating
value = math.random(0,32)
-- print(value)
histogram[value]=histogram[value]+1
end
for i = 0, 32 do
print(i,":",histogram[i])
end