Struct solana_rbpf::syscalls::BpfRand
source · [−]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
Auto Trait Implementations
impl RefUnwindSafe for BpfRand
impl Send for BpfRand
impl Sync for BpfRand
impl Unpin for BpfRand
impl UnwindSafe for BpfRand
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more