comparison utils/src/prng.rs @ 757:21b186be2590

Split the Rust version into multiple crates.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 05 Jan 2021 02:16:32 +0100
parents src/util/prng.rs@ac092b70c39a
children
comparison
equal deleted inserted replaced
756:4d91790cf8ab 757:21b186be2590
1 //! Random number generator extracted from EoSD.
2
3 /// Pseudo-random number generator from EoSD.
4 #[derive(Debug)]
5 pub struct Prng {
6 seed: u16,
7 }
8
9 impl Prng {
10 /// Create a new pseudo-random number generator from this seed.
11 pub fn new(seed: u16) -> Prng {
12 Prng {
13 seed,
14 }
15 }
16
17 /// Generates a pseudo-random u16.
18 ///
19 /// RE’d from 102h.exe@0x41e780
20 pub fn get_u16(&mut self) -> u16 {
21 let x = (self.seed ^ 0x9630).wrapping_sub(0x6553);
22 self.seed = ((x & 0xc000) >> 14) | (x << 2);
23 self.seed
24 }
25
26 /// Combines two u16 into a single u32.
27 ///
28 /// RE’d from 102h.exe@0x41e7f0
29 pub fn get_u32(&mut self) -> u32 {
30 ((self.get_u16() as u32) << 16) | self.get_u16() as u32
31 }
32
33 /// Transforms an u32 into a f64.
34 ///
35 /// RE’d from 102h.exe@0x41e820
36 pub fn get_f64(&mut self) -> f64 {
37 self.get_u32() as f64 / (0x100000000u64 as f64)
38 }
39 }