[][src]Function solana_rbpf::helpers::rand

pub fn rand<E: UserDefinedError>(
    min: u64,
    max: u64,
    _arg3: u64,
    _arg4: u64,
    _arg5: u64,
    _ro_regions: &[MemoryRegion],
    _rw_regions: &[MemoryRegion]
) -> Result<u64, EbpfError<E>>

Returns a random u64 value comprised between min and max values (inclusive). Arguments 3 to 5 are unused.

Relies on rand() function from libc, so libc::srand() should be called once before this helper is used.

Examples

extern crate libc;
extern crate solana_rbpf;
extern crate time;

use solana_rbpf::helpers::rand;
use solana_rbpf::memory_region::MemoryRegion;
use solana_rbpf::user_error::UserError;

unsafe {
    libc::srand(time::precise_time_ns() as u32)
}

let regions = [MemoryRegion::default()];
let n = rand::<UserError>(3, 6, 0, 0, 0, &regions, &regions).unwrap();
assert!(3 <= n && n <= 6);