■ 단일 파일에서 복수 모듈를 사용하는 방법을 보여준다.
▶ 예제 코드 (RS)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
mod random { pub mod linear { 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; } } pub mod xorshift { 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; } } } use random::{linear, xorshift}; fn main() { let mut seed1 : u32 = 12345u32; let mut seed2 : u32 = 12345u32; for i in 0..10 { let random_value1 : u32 = linear::get_random_value (&mut seed1) % 6 + 1; let random_value2 : u32 = xorshift::get_random_value(&mut seed2) % 6 + 1; println!("{:2} : {}, {}", i + 1, random_value1, random_value2); } } /* 1 : 1, 3 2 : 4, 4 3 : 3, 5 4 : 4, 3 5 : 5, 6 6 : 6, 1 7 : 5, 1 8 : 2, 3 9 : 3, 5 10 : 6, 5 */ |