random

Random number generation.

def random = use('random')

Functions

random.random()

Returns a number between 0 (inclusive) and 1 (exclusive).

random.random_fit()

Returns a random 53-bit whole number, from 0 (inclusive) to 9007199254740991 (inclusive). This is the raw draw that random.random() scales into [0, 1).

random.random_whole(max)

Returns a whole number from 0 (inclusive) to max (exclusive).

random.random_range(min, max)

Returns a number from min (inclusive) to max (exclusive). The result is not rounded — use random_whole when you want a whole number.

Examples

def random = use('random')

// Random boolean
var coin_flip = random.random() < 0.5

// Random element from array
var pick = function(arr) {
  return arr[random.random_whole(length(arr))]
}

var colors = ["red", "green", "blue"]
var color = pick(colors)

// Shuffle array
var shuffle = function(arr) {
  var result = array(arr)  // copy
  var i = length(result) - 1
  var j = 0
  var temp = null
  for (i = length(result) - 1; i > 0; i--) {
    j = random.random_whole(i + 1)
    temp = result[i]
    result[i] = result[j]
    result[j] = temp
  }
  return result
}

// Random in range
var x = random.random_range(-10, 10)  // -10 to 10