Browse Source

using own rng

master
Inderjit Gill 2 years ago
parent
commit
c78a391c1c
4 changed files with 45 additions and 28 deletions
  1. +0
    -1
      Cargo.toml
  2. +12
    -12
      src/grammar.rs
  3. +0
    -3
      src/lib.rs
  4. +33
    -12
      src/rng.rs

+ 0
- 1
Cargo.toml View File

@ -7,4 +7,3 @@ edition = "2018"
[dependencies]
serde_json = "1.0"
xorshift = "0.1"

+ 12
- 12
src/grammar.rs View File

@ -226,7 +226,7 @@ mod tests {
{
let g: Grammar = get_grammar();
let res = g.generate_sentence("#first-name#", &mut rng, None).unwrap();
assert_eq!(res, "Aaron");
assert_eq!(res, "Zachary");
}
{
@ -234,13 +234,13 @@ mod tests {
let res = g
.generate_sentence("#first-name# #last-name#", &mut rng, None)
.unwrap();
assert_eq!(res, "Zachary Jones");
assert_eq!(res, "Aaron Brown");
}
{
let g: Grammar = get_grammar();
let res = g.generate_sentence("#full-name#", &mut rng, None).unwrap();
assert_eq!(res, "Dylan Jones");
assert_eq!(res, "Aaron Williams III");
}
{
@ -248,21 +248,21 @@ mod tests {
let mut res = g
.generate_sentence("#citrus-amount#", &mut rng, None)
.unwrap();
assert_eq!(res, "some oranges");
assert_eq!(res, "3 oranges");
res = g
.generate_sentence("#citrus-amount#", &mut rng, None)
.unwrap();
assert_eq!(res, "an armful of limes");
assert_eq!(res, "an armful of oranges");
res = g
.generate_sentence("#citrus-amount#", &mut rng, None)
.unwrap();
assert_eq!(res, "an armful of oranges");
assert_eq!(res, "3 pineapples");
}
}
#[test]
fn with_bindings() {
let mut rng = rng::RandomIndexXor::new(1020323, 452482);
let mut rng = rng::RandomIndexXor::new(102323, 452482);
let data = r###"{
"full-name": ["#first-name# #last-name#",
"10%#first-name# #last-name# Jr.",
@ -277,7 +277,7 @@ mod tests {
let res = g
.generate_sentence("#[hero:#full-name#]story#", &mut rng, None)
.unwrap();
assert_eq!(res, "Aaron Jones is called Aaron Jones");
assert_eq!(res, "Zachary Brown is called Zachary Brown");
}
#[test]
@ -314,20 +314,20 @@ mod tests {
.unwrap();
assert_eq!(
res,
"sleeping with avacado in your bed rebalances your hormones"
"putting kombucha up your nose helps you sleep"
);
res = g
.generate_sentence("#fake-health-headline#", &mut rng, None)
.unwrap();
assert_eq!(res, "putting kombucha up your nose helps you sleep");
assert_eq!(res, "sleeping with essential oils on your face unclogs arteries");
res = g
.generate_sentence("#fake-health-headline#", &mut rng, None)
.unwrap();
assert_eq!(
res,
"sleeping with essential oils on your face unclogs arteries"
"sprinkling healing crystals in your sock fights muscle cramps"
);
res = g
@ -335,7 +335,7 @@ mod tests {
.unwrap();
assert_eq!(
res,
"sprinkling healing crystals in your sock fights muscle cramps"
"visualising epsom salt on your third eye fixes back pain"
);
}
}

+ 0
- 3
src/lib.rs View File

@ -1,6 +1,3 @@
extern crate serde_json;
extern crate xorshift;
pub mod english_modifier;
pub mod error;
pub mod grammar;


+ 33
- 12
src/rng.rs View File

@ -1,4 +1,4 @@
use xorshift::{Rng, SeedableRng, Xorshift128};
use std::num::Wrapping as wrap;
pub trait RandomIndex {
fn random_index(&mut self, len: i32) -> i32;
@ -13,22 +13,43 @@ impl RandomIndex for RandomIndexAlwaysZero {
}
pub struct RandomIndexXor {
rng: Xorshift128,
seed0: u64,
seed1: u64,
}
impl RandomIndexXor {
pub fn new(seed0: u64, seed1: u64) -> Self {
let states = [seed0, seed1];
let mut rng = RandomIndexXor {
seed0,
seed1,
};
RandomIndexXor {
rng: SeedableRng::from_seed(&states[..]),
}
// warm up
rng.next_u64();
rng.next_u64();
rng.next_u64();
rng.next_u64();
rng.next_u64();
rng
}
#[inline]
pub fn next_u64(&mut self) -> u64 {
let mut s1 = wrap(self.seed0);
let s0 = wrap(self.seed1);
let result = s0 + s1;
self.seed0 = s0.0;
s1 ^= s1 << 23;
self.seed1 = (s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5)).0;
result.0
}
}
impl RandomIndex for RandomIndexXor {
fn random_index(&mut self, len: i32) -> i32 {
((self.rng.next_u64() % (len as u64)) as i32)
((self.next_u64() % (len as u64)) as i32)
}
}
@ -40,11 +61,6 @@ mod tests {
fn random_index_xor() {
let mut rng = RandomIndexXor::new(1020323, 452482);
assert_eq!(rng.random_index(6), 3);
assert_eq!(rng.random_index(6), 2);
assert_eq!(rng.random_index(6), 4);
assert_eq!(rng.random_index(6), 2);
assert_eq!(rng.random_index(6), 1);
assert_eq!(rng.random_index(6), 4);
assert_eq!(rng.random_index(6), 2);
assert_eq!(rng.random_index(6), 0);
@ -52,5 +68,10 @@ mod tests {
assert_eq!(rng.random_index(6), 5);
assert_eq!(rng.random_index(6), 1);
assert_eq!(rng.random_index(6), 4);
assert_eq!(rng.random_index(6), 3);
assert_eq!(rng.random_index(6), 3);
assert_eq!(rng.random_index(6), 2);
assert_eq!(rng.random_index(6), 0);
assert_eq!(rng.random_index(6), 0);
}
}

Loading…
Cancel
Save