labast/src/instructions/_unix_random.rs
n3taeli 4fb06f8f34
update v1.0.3
* fixed _unix_random bug
* add custom instruction "random"
* update to spec 1.12
* more cli options
2024-02-06 19:59:43 +02:00

14 lines
404 B
Rust

use std::fs::File;
use std::io::Read;
use crate::RunError;
#[cfg(target_family = "unix")]
pub fn _unix_random(memory: &mut Vec<u16>) {
let mut rng = File::open("/dev/urandom").unwrap();
let mut buffer = [0u8; 2];
rng.read_exact(&mut buffer).unwrap();
let data = &memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
memory.push((u16::from_ne_bytes(buffer)) % (*data - 1));
}