Module 0x2::random
This module provides functionality for generating secure randomness.
- Resource
Random
- Struct
RandomInner
- Struct
RandomGenerator
- Constants
- Function
create
- Function
load_inner_mut
- Function
load_inner
- Function
update_randomness_state
- Function
new_generator
- Function
derive_next_block
- Function
fill_buffer
- Function
generate_bytes
- Function
u256_from_bytes
- Function
generate_u256
- Function
generate_u128
- Function
generate_u64
- Function
generate_u32
- Function
generate_u16
- Function
generate_u8
- Function
generate_bool
- Function
u128_in_range
- Function
generate_u128_in_range
- Function
generate_u64_in_range
- Function
generate_u32_in_range
- Function
generate_u16_in_range
- Function
generate_u8_in_range
- Function
shuffle
use 0x1::bcs;
use 0x1::vector;
use 0x2::address;
use 0x2::hmac;
use 0x2::object;
use 0x2::transfer;
use 0x2::tx_context;
use 0x2::versioned;
Resource Random
Singleton shared object which stores the global randomness state. The actual state is stored in a versioned inner field.
struct Random has key
Fields
Struct RandomInner
struct RandomInner has store
Fields
Struct RandomGenerator
Unique randomness generator, derived from the global randomness.
struct RandomGenerator has drop
Fields
Constants
const ENotSystemAddress: u64 = 0;
const EWrongInnerVersion: u64 = 1;
const CURRENT_VERSION: u64 = 1;
const EInvalidLength: u64 = 4;
const EInvalidRandomnessUpdate: u64 = 2;
const EInvalidRange: u64 = 3;
const RAND_OUTPUT_LEN: u16 = 32;
const U16_MAX: u64 = 65535;
Function create
Create and share the Random object. This function is called exactly once, when the Random object is first created. Can only be called by genesis or change_epoch transactions.
fun create(ctx: &mut tx_context::TxContext)
Implementation
Function load_inner_mut
fun load_inner_mut(self: &mut random::Random): &mut random::RandomInner
Implementation
Function load_inner
fun load_inner(self: &random::Random): &random::RandomInner
Implementation
Function update_randomness_state
Record new randomness. Called when executing the RandomnessStateUpdate system transaction.
fun update_randomness_state(self: &mut random::Random, new_round: u64, new_bytes: vector<u8>, ctx: &tx_context::TxContext)
Implementation
Function new_generator
Create a generator. Can be used to derive up to MAX_U16 * 32 random bytes.
Using randomness can be error-prone if you don't observe the subtleties in its correct use, for example, randomness dependent code might be exploitable to attacks that carefully set the gas budget in a way that breaks security. For more information, see: https://docs.sui.io/guides/developer/advanced/randomness-onchain
public fun new_generator(r: &random::Random, ctx: &mut tx_context::TxContext): random::RandomGenerator
Implementation
Function derive_next_block
fun derive_next_block(g: &mut random::RandomGenerator): vector<u8>
Implementation
Function fill_buffer
fun fill_buffer(g: &mut random::RandomGenerator)
Implementation
Function generate_bytes
Generate n random bytes.
public fun generate_bytes(g: &mut random::RandomGenerator, num_of_bytes: u16): vector<u8>
Implementation
Function u256_from_bytes
fun u256_from_bytes(g: &mut random::RandomGenerator, num_of_bytes: u8): u256
Implementation
Function generate_u256
Generate a u256.
public fun generate_u256(g: &mut random::RandomGenerator): u256
Implementation
Function generate_u128
Generate a u128.
public fun generate_u128(g: &mut random::RandomGenerator): u128
Implementation
Function generate_u64
Generate a u64.
public fun generate_u64(g: &mut random::RandomGenerator): u64
Implementation
Function generate_u32
Generate a u32.
public fun generate_u32(g: &mut random::RandomGenerator): u32
Implementation
Function generate_u16
Generate a u16.
public fun generate_u16(g: &mut random::RandomGenerator): u16
Implementation
Function generate_u8
Generate a u8.
public fun generate_u8(g: &mut random::RandomGenerator): u8
Implementation
Function generate_bool
Generate a boolean.
public fun generate_bool(g: &mut random::RandomGenerator): bool
Implementation
Function u128_in_range
fun u128_in_range(g: &mut random::RandomGenerator, min: u128, max: u128, num_of_bytes: u8): u128
Implementation
Function generate_u128_in_range
Generate a random u128 in [min, max] (with a bias of 2^{-64}).
public fun generate_u128_in_range(g: &mut random::RandomGenerator, min: u128, max: u128): u128
Implementation
Function generate_u64_in_range
public fun generate_u64_in_range(g: &mut random::RandomGenerator, min: u64, max: u64): u64
Implementation
Function generate_u32_in_range
Generate a random u32 in [min, max] (with a bias of 2^{-64}).
public fun generate_u32_in_range(g: &mut random::RandomGenerator, min: u32, max: u32): u32
Implementation
Function generate_u16_in_range
Generate a random u16 in [min, max] (with a bias of 2^{-64}).
public fun generate_u16_in_range(g: &mut random::RandomGenerator, min: u16, max: u16): u16
Implementation
Function generate_u8_in_range
Generate a random u8 in [min, max] (with a bias of 2^{-64}).
public fun generate_u8_in_range(g: &mut random::RandomGenerator, min: u8, max: u8): u8
Implementation
Function shuffle
Shuffle a vector using the random generator (Fisher–Yates/Knuth shuffle).
public fun shuffle<T>(g: &mut random::RandomGenerator, v: &mut vector<T>)