---
title: "random"
description: "Random number generation"
weight: 90
type: "docs"
---

Random number generation.

```javascript
var random = use('random')
```

## Functions

### random.random()

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

```javascript
random.random()  // e.g., 0.7234...
```

### random.random_fit()

Returns a random 56-bit integer in the range -36028797018963968 to 36028797018963967.

```javascript
random.random_fit()  // e.g., 12345678901234
```

### random.random_whole(max)

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

```javascript
random.random_whole(10)   // 0-9
random.random_whole(100)  // 0-99
random.random_whole(6) + 1  // dice roll: 1-6
```

## Examples

```javascript
var 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 random_range = function(lo, hi) {
  return lo + random.random() * (hi - lo)
}

var x = random_range(-10, 10)  // -10 to 10
```
