Zum Inhalt

Random Module

Crypto-backed random utilities for the server runtime. The Lua API is exposed at cv.random and forwards to JS exports that use Node's crypto module.

Availability

  • Server only (uses JS crypto). Calling on client will throw an error.

API

  • cv.random.int([min], [max])
  • No args: 0..2^31-1
  • One arg: 1..max
  • Two args: min..max
  • Inclusive range

  • cv.random.float([min], [max])

  • No args: 0..1
  • One arg: 0..max
  • Two args: min..max

  • cv.random.bytes(length, [encoding])

  • length must be > 0
  • encoding: "hex" (default) or "base64"

  • cv.random.uuid()

  • Returns a v4 UUID string

Examples

local roll = cv.random.int(1, 20)
local chance = cv.random.float()
local token = cv.random.bytes(16)
local id = cv.random.uuid()

Notes

  • Use this instead of math.random whenever you need unpredictable tokens, IDs, or security-sensitive randomness.