/Pony

# Random

[Source]

The `Random` trait should be implemented by all random number generators. The only method you need to implement is `fun ref next(): 64`. Once that method has been implemented, the `Random` trait provides default implementations of conversions to other number types.

```trait ref Random
```

## Constructors

### create

[Source]

Create with the specified seed. Returned values are deterministic for a given seed.

```new ref create(
x: U64 val = 5489,
y: U64 val = 0)
: Random ref^
```

#### Parameters

• x: U64 val = 5489
• y: U64 val = 0

## Public Functions

### has_next

[Source]

If used as an iterator, this always has another value.

```fun tag has_next()
: Bool val
```

### next

[Source]

A random integer in [0, 2^64)

```fun ref next()
: U64 val
```

### u8

[Source]

A random integer in [0, 2^8)

```fun ref u8()
: U8 val
```

### u16

[Source]

A random integer in [0, 2^16)

```fun ref u16()
: U16 val
```

### u32

[Source]

A random integer in [0, 2^32)

```fun ref u32()
: U32 val
```

### u64

[Source]

A random integer in [0, 2^64)

```fun ref u64()
: U64 val
```

### u128

[Source]

A random integer in [0, 2^128)

```fun ref u128()
: U128 val
```

### ulong

[Source]

A random integer in [0, ULong.max_value()]

```fun ref ulong()
: ULong val
```

### usize

[Source]

A random integer in [0, USize.max_value()]

```fun ref usize()
: USize val
```

### i8

[Source]

A random integer in [-2^7, 2^7)

```fun ref i8()
: I8 val
```

### i16

[Source]

A random integer in [-2^15, 2^15)

```fun ref i16()
: I16 val
```

### i32

[Source]

A random integer in [-2^31, 2^31)

```fun ref i32()
: I32 val
```

### i64

[Source]

A random integer in [-2^63, 2^63)

```fun ref i64()
: I64 val
```

### i128

[Source]

A random integer in [-2^127, 2^127)

```fun ref i128()
: I128 val
```

### ilong

[Source]

A random integer in [ILong.min_value(), ILong.max_value()]

```fun ref ilong()
: ILong val
```

### isize

[Source]

A random integer in [ISize.min_value(), ISize.max_value()]

```fun ref isize()
: ISize val
```

### int_fp_mult[optional N: ((U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val) & Real[N] val)]

[Source]

A random integer in [0, n)

```fun ref int_fp_mult[optional N: ((U8 val | U16 val | U32 val |
U64 val | U128 val | ULong val |
USize val) & Real[N] val)](
n: N)
: N
```

• n: N

• N

### int[optional N: ((U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val) & Real[N] val)]

[Source]

A random integer in [0, n)

Uses fixed-point inversion if platform supports native 128 bit operations otherwise uses floating-point multiplication.

```fun ref int[optional N: ((U8 val | U16 val | U32 val |
U64 val | U128 val | ULong val |
USize val) & Real[N] val)](
n: N)
: N
```

• n: N

• N

### int_unbiased[optional N: ((U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val) & Real[N] val)]

[Source]

A random integer in [0, n)

Not biased with small values of `n` like `int`.

```fun ref int_unbiased[optional N: ((U8 val | U16 val | U32 val |
U64 val | U128 val | ULong val |
USize val) & Real[N] val)](
n: N)
: N
```

• n: N

• N

### real

[Source]

A random number in [0, 1)

```fun ref real()
: F64 val
```

### shuffle[A: A]

[Source]

Shuffle the elements of the array into a random order, mutating the array.

```fun ref shuffle[A: A](
array: Array[A] ref)
: None val
```

## Private Functions

### _u64_unbiased

[Source]

Generates a U64 in the range `[0, n)` while avoiding bias.

See: - https://arxiv.org/abs/1805.10941 - http://www.pcg-random.org/posts/bounded-rands.html

```fun ref _u64_unbiased(
range: U64 val)
: U64 val
```

#### Parameters

• range: U64 val