Last active
May 2, 2022 09:25
-
-
Save xpepermint/f9a0ab323c48de6440eb6b79ffd8456d to your computer and use it in GitHub Desktop.
Rust random number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs::File; | |
use std::io::Read; | |
fn main() { | |
let mut f = File::open("/dev/urandom").unwrap(); // from Linux | |
let mut buf = [0u8; 16]; | |
f.read_exact(&mut buf).unwrap(); | |
println!("{:?}", buf); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("{:?}", get_random_buf()); | |
} | |
fn get_random_buf() -> Result<[u8; 32], getrandom::Error> { | |
let mut buf = [0u8; 32]; | |
getrandom::getrandom(&mut buf)?; | |
Ok(buf) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
unsafe { | |
srand(); | |
println!("{}", rand()); | |
} | |
} | |
extern "C" { | |
fn srand() -> u32; | |
fn rand() -> u32; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment