■ Xorshift 알고리즘을 사용해 난수를 생성하는 방법을 보여준다.
▶ 예제 코드 (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 |
use std::time; static mut SEED : u32 = 0; unsafe fn get_random_value(start : u32, end : u32) -> u32 { if SEED == 0 { let epoc : time::Duration = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap(); SEED = epoc.as_millis() as u32; } SEED ^= SEED << 13; SEED ^= SEED >> 17; SEED ^= SEED << 5; return SEED % (end - start + 1) + start; } fn main() { for _ in 0..100 { unsafe { let random_value : u32 = get_random_value(1, 6); println!("{}", random_value); } } } |