logo
pub struct BpfRand {}
Expand description

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 syscall is used.

Examples

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

use solana_rbpf::syscalls::{BpfRand, Result};
use solana_rbpf::memory_region::{MemoryRegion, MemoryMapping};
use solana_rbpf::vm::{Config, SyscallObject};
use solana_rbpf::user_error::UserError;

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

let mut result: Result = Ok(0);
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(vec![], &config).unwrap();
BpfRand::call(&mut BpfRand {}, 3, 6, 0, 0, 0, &memory_mapping, &mut result);
let n = result.unwrap();
assert!(3 <= n && n <= 6);

Trait Implementations

Call the syscall function

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.