■ 모듈을 파일로 분리해서 사용하는 방법을 보여준다.
▶ src/random/linear.rs
1 2 3 4 5 6 7 8 9 10 11 12 |
use std::num::Wrapping; pub fn get_random_value(seed : &mut u32) -> u32 { let (a, c) = (134775813u32, 12345u32); *seed = (Wrapping(*seed) * Wrapping(a) + Wrapping(c)).0; return *seed; } |
▶ src/random/xorshift.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
pub fn get_random_value(seed : &mut u32) -> u32 { let mut y : u32 = *seed; y ^= y << 13; y ^= y >> 17; y ^= y << 5; *seed = y; return y; } |
▶ src/random/mod.rs
1 2 3 4 |
pub mod linear; pub mod xorshift; |
▶ src/main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
mod random; use crate::random::{linear, xorshift}; fn main() { let mut seed : u32 = 1u32; let random_value1 : u32 = linear::get_random_value(&mut seed); let random_value2 : u32 = xorshift::get_random_value(&mut seed); println!("{}, {}", random_value1, random_value2); } |