File: utils.lua

package info (click to toggle)
lua-busted 2.0~rc12-1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 656 kB
  • ctags: 310
  • sloc: sh: 191; makefile: 6
file content (26 lines) | stat: -rw-r--r-- 546 bytes parent folder | download
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
return {
  split = require 'pl.utils'.split,

  shuffle = function(t, seed)
    if seed then math.randomseed(seed) end
    local n = #t
    while n >= 2 do
      local k = math.random(n)
      t[n], t[k] = t[k], t[n]
      n = n - 1
    end
    return t
  end,

  urandom = function()
    local f = io.open('/dev/urandom', 'rb')
    if not f then return nil end
    local s = f:read(4) f:close()
    local bytes = {s:byte(1, 4)}
    local value = 0
    for _, v in ipairs(bytes) do
      value = value * 256 + v
    end
    return value
  end,
}