Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Lua中计算表中整数频率的最佳方法是什么?_Lua - Fatal编程技术网

在Lua中计算表中整数频率的最佳方法是什么?

在Lua中计算表中整数频率的最佳方法是什么?,lua,Lua,这是我用来打印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.rando

这是我用来打印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={}
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

更简单的是做一个柱状图(按值索引的表格)。每当遇到一个值时,直方图[值]就会递增

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