Browse Source

removed rng crates with own version based on xorshift

master
Inderjit Gill 6 months ago
parent
commit
9b7b2d78d1
8 changed files with 244 additions and 254 deletions
  1. +0
    -3
      core/Cargo.toml
  2. +1
    -1
      core/src/bitmap.rs
  3. +84
    -95
      core/src/gene.rs
  4. +18
    -18
      core/src/geometry/stroked_bezier_rect.rs
  5. +16
    -16
      core/src/native.rs
  6. +100
    -99
      core/src/prng.rs
  7. +16
    -21
      core/src/unparser.rs
  8. +9
    -1
      server/static/gallery.json

+ 0
- 3
core/Cargo.toml View File

@@ -5,10 +5,7 @@ authors = ["Inderjit Gill <email@indy.io>"]
license = "GPL-3.0+"
edition = "2018"


[dependencies]
rand_core = "0.4.0"
rand_xorshift = "0.1.1"
strum = "0.13.0"
strum_macros = "0.13.0"
log = "0.4"

+ 1
- 1
core/src/bitmap.rs View File

@@ -138,7 +138,7 @@ pub fn each(
// invariant: elements with index >= i have been locked in place.
i -= 1;
// lock element i in place.
coords.swap(i, prng.gen_range(0, i as u32 + 1) as usize);
coords.swap(i, prng.next_u32_range(0, i as u32 + 1) as usize);
}

for coord in coords {

+ 84
- 95
core/src/gene.rs View File

@@ -176,7 +176,7 @@ impl Genotype {

genotypes.push(Genotype::build_from_initial_values(trait_list)?);
for _ in 1..population_size {
let genotype_seed = prng.prng_f32_defined_range() as i32;
let genotype_seed = prng.next_f32_defined_range() as i32;
genotypes.push(Genotype::build_from_seed(trait_list, genotype_seed)?);
}

@@ -220,7 +220,7 @@ impl Genotype {
let mut child: Genotype = Default::default();

let num_genes = self.genes.len();
let crossover_index: usize = prng.prng_usize_range(0, num_genes);
let crossover_index: usize = prng.next_usize_range(0, num_genes);

for i in 0..crossover_index {
child.push_gene(self.genes[i].clone())
@@ -242,7 +242,7 @@ impl Genotype {
let num_genes = self.genes.len();

for i in 0..num_genes {
let r = prng.prng_f32();
let r = prng.next_f32();
if r < mutation_rate {
self.gene_generate_new_var(i, prng, trait_list)?;
}
@@ -321,11 +321,11 @@ pub fn next_generation(

for _ in 0..(population_size - num_parents) {
// select 2 different parents
let a_index = rng.prng_usize_range(0, num_parents - 1);
let a_index = rng.next_usize_range(0, num_parents - 1);

let mut b_index = a_index;
for _ in 0..retry_count {
b_index = rng.prng_usize_range(0, num_parents - 1);
b_index = rng.next_usize_range(0, num_parents - 1);
if b_index != a_index {
break;
}
@@ -471,8 +471,8 @@ mod tests {
let genotype = Genotype::build_from_seed(&trait_list, 432).unwrap();

assert_eq!(genotype.genes.len(), 2);
gene_float(&genotype.genes[0], 8.82988);
gene_float(&genotype.genes[1], 6.2474613);
gene_float(&genotype.genes[0], 6.74415);
gene_float(&genotype.genes[1], 7.869932);
}

#[test]
@@ -483,8 +483,8 @@ mod tests {
let genotype = Genotype::build_from_seed(&trait_list, 432).unwrap();

assert_eq!(genotype.genes.len(), 2);
gene_2d(&genotype.genes[0], 59.75697, 56.067802);
gene_2d(&genotype.genes[1], 55.85068, 57.474014);
gene_2d(&genotype.genes[0], 56.77736, 58.385616);
gene_2d(&genotype.genes[1], 51.329006, 58.351044);
}

fn is_float(var: &Var, expected: f32) {
@@ -532,19 +532,19 @@ mod tests {
is_keyword(&res, Keyword::Transformers);

{
let (res, genotype) = run_with_seeded_genotype(s, 56534).unwrap();
let (res, genotype) = run_with_seeded_genotype(s, 6534).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_keyword(&res, Keyword::KnightRider);
is_keyword(&res, Keyword::Chrome);
}
{
let (res, genotype) = run_with_seeded_genotype(s, 6534).unwrap();
let (res, genotype) = run_with_seeded_genotype(s, 1534).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_keyword(&res, Keyword::HotlineMiami);
is_keyword(&res, Keyword::KnightRider);
}
{
let (res, genotype) = run_with_seeded_genotype(s, 2009).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_keyword(&res, Keyword::Rainbow);
is_keyword(&res, Keyword::Mars);
}
}

@@ -555,24 +555,21 @@ mod tests {
let res = compile_and_execute(s).unwrap();
is_float(&res, 12.3);

// a = 222 b = 223 c = 224 d = 225
{
let (res, genotype) = run_with_seeded_genotype(s, 211).unwrap(); // 222
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 12.3);
let (res, genotype) = run_with_seeded_genotype(s, 211).unwrap(); // 222
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 12.3);

let (res, genotype) = run_with_seeded_genotype(s, 25).unwrap(); // 224
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 14.5);
let (res, genotype) = run_with_seeded_genotype(s, 25).unwrap(); // 224
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 12.3);

let (res, genotype) = run_with_seeded_genotype(s, 37).unwrap(); // 223
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 13.4);
let (res, genotype) = run_with_seeded_genotype(s, 37).unwrap(); // 223
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 14.5);

let (res, genotype) = run_with_seeded_genotype(s, 45).unwrap(); // 225
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 15.6);
}
let (res, genotype) = run_with_seeded_genotype(s, 45).unwrap(); // 225
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 15.6);
}

fn assert_native_opcode_in_program(program: &Program, native: Native) {
@@ -590,25 +587,19 @@ mod tests {
}

#[test]
fn gen_select_natives() {
fn gen_select_natives_xx() {
let s = "({rect (gen/select from: '(rect circle circle-slice))} position: [100 100])";
{
// dbg!(Native::Rect as i32);
// seed 6 == genotype[0].name: 305 Rect
let (program, _genotype) = program_with_seeded_genotype(s, 6).unwrap();
let (program, _genotype) = program_with_seeded_genotype(s, 7).unwrap();
assert_native_opcode_in_program(&program, Native::Rect);
}
{
// dbg!(Native::Circle as i32);
// seed 5 == genotype[0].name: 306 Circle
let (program, _genotype) = program_with_seeded_genotype(s, 5).unwrap();
assert_native_opcode_in_program(&program, Native::Circle);
let (program, _genotype) = program_with_seeded_genotype(s, 5800).unwrap();
assert_native_opcode_in_program(&program, Native::CircleSlice);
}
{
// dbg!(Native::CircleSlice as i32);
// seed 14 == genotype[0].name: 307 CircleSlice
let (program, _genotype) = program_with_seeded_genotype(s, 14).unwrap();
assert_native_opcode_in_program(&program, Native::CircleSlice);
let (program, _genotype) = program_with_seeded_genotype(s, 19).unwrap();
assert_native_opcode_in_program(&program, Native::Circle);
}
}

@@ -619,23 +610,21 @@ mod tests {
let res = compile_and_execute(s).unwrap();
is_float(&res, 12.3);

{
let (res, genotype) = run_with_seeded_genotype(s, 211).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 12.3);
let (res, genotype) = run_with_seeded_genotype(s, 211).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 12.3);

let (res, genotype) = run_with_seeded_genotype(s, 25).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 14.5);
let (res, genotype) = run_with_seeded_genotype(s, 27).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 13.4);

let (res, genotype) = run_with_seeded_genotype(s, 37).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 13.4);
let (res, genotype) = run_with_seeded_genotype(s, 37).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 14.5);

let (res, genotype) = run_with_seeded_genotype(s, 45).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 15.6);
}
let (res, genotype) = run_with_seeded_genotype(s, 45).unwrap();
assert_eq!(genotype.genes.len(), 1);
is_float(&res, 15.6);
}

// bug_gen_select_custom_locals
@@ -653,7 +642,7 @@ mod tests {
let (res, genotype) = run_with_seeded_genotype(s, seed).unwrap();
assert_eq!(genotype.genes.len(), genotype_length);

is_float(&res, 4.4);
is_float(&res, 1.1);
}

#[test]
@@ -666,41 +655,41 @@ mod tests {
let (res, genotype) = run_with_seeded_genotype(s, 432).unwrap();
is_col(
&res,
&Colour::new(ColourFormat::Rgb, 0.97569704, 0.6067802, 0.585068, 0.3),
&Colour::new(ColourFormat::Rgb, 0.67773575, 0.8385617, 0.13290067, 0.3),
);
assert_eq!(genotype.genes.len(), 1);
}

#[test]
fn genotype_compile() {
fn genotype_compile_xxx() {
geno_test(
"(+ {3 (gen/scalar min: 10 max: 20)} {4 (gen/scalar min: 100 max: 105)})",
432,
2,
7.0,
122.79086,
120.97017,
);
geno_test("(+ 6 {3 (gen/int min: 1 max: 100)})", 432, 1, 9.0, 104.0);
geno_test("(+ 6 {3 (gen/int min: 1 max: 100)})", 432, 1, 9.0, 74.0);
geno_test(
"(+ 6 {3 (gen/scalar min: 1 max: 100)})",
432,
1,
9.0,
103.59401,
74.09584,
);
geno_test("(+ 6 {3 (gen/int min: 1 max: 100)})", 874, 1, 9.0, 60.0);
geno_test("(+ 6 {3 (gen/int min: 1 max: 100)})", 874, 1, 9.0, 81.0);
geno_test(
"(+ 6 {3 (gen/scalar min: 1 max: 100)})",
874,
1,
9.0,
59.47561,
81.0833,
);
}

#[test]
fn genotype_compile_stray() {
geno_test("{3 (gen/stray from: 3 by: 0.5)}", 432, 1, 3.0, 3.475697);
geno_test("{3 (gen/stray from: 3 by: 0.5)}", 432, 1, 3.0, 3.1777358);
geno_test("{3 (gen/stray-int from: 3 by: 0.5)}", 432, 1, 3.0, 3.0);
}

@@ -712,7 +701,7 @@ mod tests {
7524,
2,
(100.0, 200.0),
(93.04805, 197.49728),
(94.410095, 202.5176),
);
}

@@ -737,9 +726,9 @@ mod tests {
let (res, genotype) = run_with_seeded_genotype(expr, seed).unwrap();
if let Var::Vector(vs) = res {
assert_eq!(vs.len(), 3);
is_2d(&vs[0], (0.9825403, 0.85869956));
is_2d(&vs[1], (0.59191173, 0.999328));
is_2d(&vs[2], (0.22858822, 0.4880673));
is_2d(&vs[0], (0.9590588, 0.9022932));
is_2d(&vs[1], (0.8897112, 0.013709899));
is_2d(&vs[2], (0.85696673, 0.5854448));
} else {
assert!(false);
}
@@ -765,9 +754,9 @@ mod tests {
let (res, genotype) = run_with_seeded_genotype(expr, seed).unwrap();
if let Var::Vector(vs) = res {
assert_eq!(vs.len(), 3);
is_2d(&vs[0], (59.8254, 58.586998));
is_2d(&vs[1], (55.919117, 59.99328));
is_2d(&vs[2], (52.28588, 54.880672));
is_2d(&vs[0], (59.590588, 59.022934));
is_2d(&vs[1], (58.89711, 50.1371));
is_2d(&vs[2], (58.569668, 55.854446));
} else {
assert!(false);
}
@@ -794,9 +783,9 @@ mod tests {
let (res, genotype) = run_with_seeded_genotype(expr, seed).unwrap();
if let Var::Vector(vs) = res {
assert_eq!(vs.len(), 3);
is_float(&vs[0], 0.6279464);
is_float(&vs[1], 0.46001887);
is_float(&vs[2], 0.51953447);
is_float(&vs[0], 0.12203506);
is_float(&vs[1], 0.8389967);
is_float(&vs[2], 0.6913055);
} else {
assert!(false);
}
@@ -818,44 +807,44 @@ mod tests {
let (_, genotype_b) = run_with_seeded_genotype(expr, seed_b).unwrap();

assert_eq!(genotype_a.genes.len(), 3);
gene_float(&genotype_a.genes[0], 0.000778846);
gene_float(&genotype_a.genes[1], 0.5599265);
gene_float(&genotype_a.genes[2], 0.8937246);
gene_float(&genotype_a.genes[0], 0.12962966);
gene_float(&genotype_a.genes[1], 0.66991657);
gene_float(&genotype_a.genes[2], 0.056645457);

assert_eq!(genotype_b.genes.len(), 3);
gene_float(&genotype_b.genes[0], 0.1344259);
gene_float(&genotype_b.genes[1], 0.052326918);
gene_float(&genotype_b.genes[2], 0.024050714);
gene_float(&genotype_b.genes[0], 0.49113372);
gene_float(&genotype_b.genes[1], 0.8261006);
gene_float(&genotype_b.genes[2], 0.9936072);

let parents = vec![genotype_a, genotype_b];
let children = next_generation(&parents, 5, 0.2, 234, &trait_list).unwrap();

// first 2 children should be clones of the parents
assert_eq!(children[0].genes.len(), 3);
gene_float(&children[0].genes[0], 0.000778846);
gene_float(&children[0].genes[1], 0.5599265);
gene_float(&children[0].genes[2], 0.8937246);
gene_float(&children[0].genes[0], 0.12962966);
gene_float(&children[0].genes[1], 0.66991657);
gene_float(&children[0].genes[2], 0.056645457);

assert_eq!(children[1].genes.len(), 3);
gene_float(&children[1].genes[0], 0.1344259);
gene_float(&children[1].genes[1], 0.052326918);
gene_float(&children[1].genes[2], 0.024050714);
gene_float(&children[1].genes[0], 0.49113372);
gene_float(&children[1].genes[1], 0.8261006);
gene_float(&children[1].genes[2], 0.9936072);

// 3 children
assert_eq!(children[2].genes.len(), 3);
gene_float(&children[2].genes[0], 0.6867611); // mutation
gene_float(&children[2].genes[1], 0.052326918); // b
gene_float(&children[2].genes[2], 0.024050714); // b
gene_float(&children[2].genes[0], 0.49113372); // mutation
gene_float(&children[2].genes[1], 0.8261006); // b
gene_float(&children[2].genes[2], 0.9936072); // b

assert_eq!(children[3].genes.len(), 3);
gene_float(&children[3].genes[0], 0.000778846); // a
gene_float(&children[3].genes[1], 0.5599265); // a
gene_float(&children[3].genes[2], 0.024050714); // b
gene_float(&children[3].genes[0], 0.12962966); // a
gene_float(&children[3].genes[1], 0.8261006); // a
gene_float(&children[3].genes[2], 0.463081); // b

assert_eq!(children[4].genes.len(), 3);
gene_float(&children[4].genes[0], 0.000778846); // a
gene_float(&children[4].genes[1], 0.052326918); // b
gene_float(&children[4].genes[2], 0.024050714); // b
gene_float(&children[4].genes[0], 0.21231069); // a
gene_float(&children[4].genes[1], 0.29541624); // b
gene_float(&children[4].genes[2], 0.9936072); // b

assert_eq!(children.len(), 5);
}

+ 18
- 18
core/src/geometry/stroked_bezier_rect.rs View File

@@ -74,14 +74,14 @@ pub fn render(
let h = y_start + stroke_half_thickness + (i as f32 * stroke_offset_factor);

let coords: [f32; 8] = [
(prng.prng_f32_range(-1.0, 1.0) * vol) + x_start + (0.0 * th_width),
h + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.prng_f32_range(-1.0, 1.0) * vol) + x_start + (1.0 * th_width),
h + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.prng_f32_range(-1.0, 1.0) * vol) + x_start + (2.0 * th_width),
h + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.prng_f32_range(-1.0, 1.0) * vol) + x_start + (3.0 * th_width),
h + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + x_start + (0.0 * th_width),
h + (prng.next_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + x_start + (1.0 * th_width),
h + (prng.next_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + x_start + (2.0 * th_width),
h + (prng.next_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + x_start + (3.0 * th_width),
h + (prng.next_f32_range(-1.0, 1.0) * vol),
];

stroked_bezier::render(
@@ -95,7 +95,7 @@ pub fn render(
stroke_thickness,
&rgb_from_lab,
colour_volatility,
prng.prng_f32(),
prng.next_f32(),
Easing::Linear,
uvm,
)?;
@@ -110,14 +110,14 @@ pub fn render(
let v = x_start + stroke_half_thickness + (i as f32 * stroke_offset_factor);

let coords: [f32; 8] = [
v + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.prng_f32_range(-1.0, 1.0) * vol) + y_start + (0.0 * th_height),
v + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.prng_f32_range(-1.0, 1.0) * vol) + y_start + (1.0 * th_height),
v + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.prng_f32_range(-1.0, 1.0) * vol) + y_start + (2.0 * th_height),
v + (prng.prng_f32_range(-1.0, 1.0) * vol),
(prng.prng_f32_range(-1.0, 1.0) * vol) + y_start + (3.0 * th_height),
v + (prng.next_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + y_start + (0.0 * th_height),
v + (prng.next_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + y_start + (1.0 * th_height),
v + (prng.next_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + y_start + (2.0 * th_height),
v + (prng.next_f32_range(-1.0, 1.0) * vol),
(prng.next_f32_range(-1.0, 1.0) * vol) + y_start + (3.0 * th_height),
];

stroked_bezier::render(
@@ -131,7 +131,7 @@ pub fn render(
stroke_thickness,
&rgb_from_lab,
colour_volatility,
prng.prng_f32(),
prng.next_f32(),
Easing::Linear,
uvm,
)?;

+ 16
- 16
core/src/native.rs View File

@@ -2062,7 +2062,7 @@ fn prng_values_execute(vm: &mut Vm) -> Result<Option<Var>> {

let mut vs: Vec<Var> = Vec::new();
for _ in 0..num {
let f = ref_mut_prng_state.prng_f32_defined_range();
let f = ref_mut_prng_state.next_f32_defined_range();
vs.push(Var::Float(f))
}

@@ -2087,7 +2087,7 @@ fn prng_value_execute(vm: &mut Vm) -> Result<Option<Var>> {
}

let mut ref_mut_prng_state = ref_mut_prng_state_struct(&vm.stack, vm.sp, 1)?;
let res = ref_mut_prng_state.prng_f32_defined_range();
let res = ref_mut_prng_state.next_f32_defined_range();

Ok(Some(Var::Float(res)))
}
@@ -3021,7 +3021,7 @@ fn gen_stray_int_execute(vm: &mut Vm) -> Result<Option<Var>> {
let by: f32 = vm.stack_peek(2)?;

let by = mathutil::absf(by);
let value = vm.prng_state.prng_f32_range(from - by, from + by);
let value = vm.prng_state.next_f32_range(from - by, from + by);
let value = value.floor();

Ok(Some(Var::Float(value)))
@@ -3044,7 +3044,7 @@ fn gen_stray_execute(vm: &mut Vm) -> Result<Option<Var>> {
let by: f32 = vm.stack_peek(2)?;

let by = mathutil::absf(by);
let value = vm.prng_state.prng_f32_range(from - by, from + by);
let value = vm.prng_state.next_f32_range(from - by, from + by);

Ok(Some(Var::Float(value)))
}
@@ -3087,7 +3087,7 @@ fn gen_stray_2d_execute(vm: &mut Vm) -> Result<Option<Var>> {
// pick a scalar between min and max
let value = vm
.prng_state
.prng_f32_range(from_index - by_index, from_index + by_index);
.next_f32_range(from_index - by_index, from_index + by_index);

Ok(Some(Var::Float(value)))
}
@@ -3141,7 +3141,7 @@ fn gen_stray_3d_execute(vm: &mut Vm) -> Result<Option<Var>> {
};

// pick a scalar between min and max
let value = vm.prng_state.prng_f32_range(from - by, from + by);
let value = vm.prng_state.next_f32_range(from - by, from + by);

Ok(Some(Var::Float(value)))
}
@@ -3195,7 +3195,7 @@ fn gen_stray_4d_execute(vm: &mut Vm) -> Result<Option<Var>> {
};

// pick a scalar between min and max
let value = vm.prng_state.prng_f32_range(from - by, from + by);
let value = vm.prng_state.next_f32_range(from - by, from + by);

Ok(Some(Var::Float(value)))
}
@@ -3217,7 +3217,7 @@ fn gen_int_execute(vm: &mut Vm) -> Result<Option<Var>> {
let max: f32 = vm.stack_peek(2)?;

// pick a scalar between min and max
let value = vm.prng_state.prng_f32_range(min, max + 1.0);
let value = vm.prng_state.next_f32_range(min, max + 1.0);

Ok(Some(Var::Float(value.floor())))
}
@@ -3239,7 +3239,7 @@ fn gen_scalar_execute(vm: &mut Vm) -> Result<Option<Var>> {
let max: f32 = vm.stack_peek(2)?;

// pick a scalar between min and max
let value = vm.prng_state.prng_f32_range(min, max);
let value = vm.prng_state.next_f32_range(min, max);

Ok(Some(Var::Float(value)))
}
@@ -3260,8 +3260,8 @@ fn gen_2d_execute(vm: &mut Vm) -> Result<Option<Var>> {
let min: f32 = vm.stack_peek(1)?;
let max: f32 = vm.stack_peek(2)?;

let x = vm.prng_state.prng_f32_range(min, max);
let y = vm.prng_state.prng_f32_range(min, max);
let x = vm.prng_state.next_f32_range(min, max);
let y = vm.prng_state.next_f32_range(min, max);

Ok(Some(Var::V2D(x, y)))
}
@@ -3284,7 +3284,7 @@ fn gen_select_execute(vm: &mut Vm) -> Result<Option<Var>> {
}

let from = stack_peek_vars(&vm.stack, vm.sp, 1)?;
let index = vm.prng_state.prng_usize_range(0, from.len());
let index = vm.prng_state.next_usize_range(0, from.len());

Ok(Some(from[index].clone()))
}
@@ -3305,14 +3305,14 @@ fn gen_col_execute(vm: &mut Vm) -> Result<Option<Var>> {
vm.stack_peek(1)?
} else {
// no alpha was given so generate a random value
vm.prng_state.prng_f32_range(0.0, 1.0)
vm.prng_state.next_f32_range(0.0, 1.0)
};

Ok(Some(Var::Colour(Colour::new(
ColourFormat::Rgb,
vm.prng_state.prng_f32_range(0.0, 1.0),
vm.prng_state.prng_f32_range(0.0, 1.0),
vm.prng_state.prng_f32_range(0.0, 1.0),
vm.prng_state.next_f32_range(0.0, 1.0),
vm.prng_state.next_f32_range(0.0, 1.0),
vm.prng_state.next_f32_range(0.0, 1.0),
alpha,
))))
}

+ 100
- 99
core/src/prng.rs View File

@@ -13,9 +13,99 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::num::Wrapping as wrap;

use crate::mathutil::{clamp, lerp};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;

#[derive(Clone, Debug)]
pub struct PrngStateStruct {
seed0: u64,
seed1: u64,
min: f32,
max: f32,
}

impl PrngStateStruct {
pub fn new(seed: i32, min: f32, max: f32) -> Self {
let mut prng = PrngStateStruct {
seed0: 0,
seed1: 0,
min,
max,
};
prng.set_state(seed);
prng
}

pub fn set_state(&mut self, seed: i32) {
self.seed0 = seed as u64 * seed as u64;
self.seed1 = (seed + 3145) as u64;

// warm up
self.next_f32();
self.next_f32();
self.next_f32();
self.next_f32();
self.next_f32();
}

pub fn clone_rng(&mut self, other: PrngStateStruct) {
self.seed0 = other.seed0;
self.seed1 = other.seed1;
self.min = other.min;
self.max = other.max;
}

// 0..1
pub fn next_f32(&mut self) -> f32 {
let a = self.next_u32();
a as f32 / std::u32::MAX as f32
}

pub fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}

#[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
}

pub fn next_u32_range(&mut self, low: u32, high: u32) -> u32 {
let a = self.next_u32();
(a % (high - low)) + low
}

pub fn next_f32_range(&mut self, min: f32, max: f32) -> f32 {
let value = self.next_f32();
(value * (max - min)) + min
}

pub fn next_usize_range(&mut self, min: usize, max: usize) -> usize {
self.next_f32_range(min as f32, max as f32) as usize
}

pub fn next_f32_defined_range(&mut self) -> f32 {
let value = self.next_f32();
(value * (self.max - self.min)) + self.min
}

pub fn next_f32_around(&mut self, val: f32, percent: f32, min: f32, max: f32) -> f32 {
let value = self.next_f32();
let range = ((max - min) / 100.0) * percent;
let lowest = val - range;
let highest = val + range;
let res = (value * (highest - lowest)) + lowest;

clamp(res, min, max)
}
}

const PERMUTATIONS: [usize; 512] = [
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69,
@@ -69,94 +159,6 @@ const INDICES: [usize; 64] = [
8, 9, 10, 11,
];

#[derive(Clone, Debug)]
pub struct PrngStateStruct {
pub rng: XorShiftRng,
min: f32,
max: f32,
}

fn as_u8(i: i32) -> u8 {
(i % (1 << 8)) as u8
}

impl PrngStateStruct {
pub fn new(seed: i32, min: f32, max: f32) -> Self {
PrngStateStruct {
rng: PrngStateStruct::make_xor_shift_rng(seed),
min,
max,
}
}

pub fn set_state(&mut self, seed: i32) {
self.rng = PrngStateStruct::make_xor_shift_rng(seed);
}

pub fn clone_rng(&mut self, other: PrngStateStruct) {
self.rng = other.rng.clone();
}

pub fn gen_range(&mut self, low: u32, high: u32) -> u32 {
let a = self.rng.next_u32();
(a % (high - low)) + low
}

// 0..1
pub fn prng_f32(&mut self) -> f32 {
let a = self.rng.next_u32();

a as f32 / std::u32::MAX as f32
}

pub fn prng_f32_range(&mut self, min: f32, max: f32) -> f32 {
let value = self.prng_f32();
(value * (max - min)) + min
}

pub fn prng_usize_range(&mut self, min: usize, max: usize) -> usize {
self.prng_f32_range(min as f32, max as f32) as usize
}

pub fn prng_f32_defined_range(&mut self) -> f32 {
let value = self.prng_f32();
(value * (self.max - self.min)) + self.min
}

pub fn prng_f32_around(&mut self, val: f32, percent: f32, min: f32, max: f32) -> f32 {
let value = self.prng_f32();
let range = ((max - min) / 100.0) * percent;
let lowest = val - range;
let highest = val + range;
let res = (value * (highest - lowest)) + lowest;

clamp(res, min, max)
}

fn make_xor_shift_rng(seed: i32) -> XorShiftRng {
let seedy: [u8; 16] = [
as_u8(seed),
as_u8(seed + 1),
as_u8(seed + seed - 2),
as_u8(seed + 3),
as_u8(seed + seed - 4),
as_u8(seed + 5),
as_u8(seed + seed - 6),
as_u8(seed + 7),
as_u8(seed + seed - 8),
as_u8(seed + 9),
as_u8(seed + seed - 10),
as_u8(seed + 11),
as_u8(seed + seed - 12),
as_u8(seed + 13),
as_u8(seed + seed - 14),
as_u8(seed + 15),
];

XorShiftRng::from_seed(seedy)
}
}

fn fade(t: f32) -> f32 {
t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}
@@ -243,7 +245,7 @@ pub mod tests {
(probe scalar: (prng/value from: p))
(probe scalar: (prng/value from: p))
(probe scalar: (prng/value from: p))",
"0.36151162 0.4412291 0.37725854 0.5783009 0.65834785 0.48318255 0.7342905 0.44944018 0.56456256",
"0.16439326 0.58795106 0.12325332 0.039127756 0.9678266 0.8247009 0.787962 0.13722154 0.94319534",
);
}

@@ -260,7 +262,7 @@ pub mod tests {
(probe scalar: (prng/value from: p))
(probe scalar: (prng/value from: p))
(probe scalar: (prng/value from: p))",
"5.3797703 7.0697656 4.541839 7.1620464 6.7335386 3.5603561 3.4189374 3.1284251 8.890152",
"7.696081 6.462363 6.579473 4.650559 5.4763083 4.6319327 8.11852 6.7570615 5.3803825",
);
}

@@ -272,7 +274,7 @@ pub mod tests {
(probe scalar: (nth from: vs n: 0))
(probe scalar: (nth from: vs n: 1))
(probe scalar: (nth from: vs n: 2))",
"0.36151162 0.4412291 0.37725854",
"0.16439326 0.58795106 0.12325332",
);
}

@@ -282,19 +284,19 @@ pub mod tests {
"(define p (prng/build seed: 5 min: 0 max: 1))
(define vs (prng/values from: p num: 3))
(nth from: vs n: 0)",
0.36151162,
0.16439326,
);
is_float(
"(define p (prng/build seed: 5 min: 0 max: 1))
(define vs (prng/values from: p num: 3))
(nth from: vs n: 1)",
0.4412291,
0.58795106,
);
is_float(
"(define p (prng/build seed: 5 min: 0 max: 1))
(define vs (prng/values from: p num: 3))
(nth from: vs n: 2)",
0.37725854,
0.12325332,
);
}

@@ -303,11 +305,10 @@ pub mod tests {
let mut prng = PrngStateStruct::new(542, 0.0, 1.0);

for _ in 0..100 {
let f = prng.prng_f32();
let f = prng.next_f32();
println!("{}", f);
}

assert_eq!(prng.prng_f32(), 0.7896869);
assert_eq!(prng.next_f32(), 0.49469042);
}

}

+ 16
- 21
core/src/unparser.rs View File

@@ -402,35 +402,35 @@ mod tests {
seeded_unparse_check(
975,
"(+ 6 {3 (gen/int min: 1 max: 50)})",
"(+ 6 {46 (gen/int min: 1 max: 50)})",
"(+ 6 {42 (gen/int min: 1 max: 50)})",
);
seeded_unparse_check(
975,
"{rainbow (gen/select from: col/procedural-fn-presets)}",
"{transformers (gen/select from: col/procedural-fn-presets)}",
"{robocop (gen/select from: col/procedural-fn-presets)}",
);
seeded_unparse_check(
342,
"[8 {3 (gen/int min: 0 max: 9)}]",
"[8 {4 (gen/int min: 0 max: 9)}]",
"[8 {5 (gen/int min: 0 max: 9)}]",
);

seeded_unparse_check(
764,
"{3.45 (gen/scalar min: 0 max: 9)}",
"{2.38 (gen/scalar min: 0 max: 9)}",
"{4.08 (gen/scalar min: 0 max: 9)}",
);

seeded_unparse_check(
764,
"{3.4 (gen/scalar min: 0 max: 9)}",
"{2.4 (gen/scalar min: 0 max: 9)}",
"{4.1 (gen/scalar min: 0 max: 9)}",
);

seeded_unparse_check(
764,
"(col/rgb r: {0.4 (gen/scalar)} g: 0.1)",
"(col/rgb r: {0.3 (gen/scalar)} g: 0.1)",
"(col/rgb r: {0.5 (gen/scalar)} g: 0.1)",
);

seeded_unparse_check(
@@ -447,30 +447,25 @@ mod tests {
(gen/int min: 80 max: 670)}) (rect position: [500 500] colour: red
width: {120 (gen/int min: 80 max: 400)} height: {140 (gen/int min: 80
max: 670)})",
"(rect position: [500 500] colour: red width: {164 (gen/int min: 80 max:
400)} height: {562 (gen/int min: 80 max: 670)}) (rect position: [500
500] colour: red width: {289 (gen/int min: 80 max: 400)} height: {663
(gen/int min: 80 max: 670)}) (rect position: [500 500] colour: red
width: {210 (gen/int min: 80 max: 400)} height: {148 (gen/int min: 80
max: 670)})",
"(rect position: [500 500] colour: red width: {225 (gen/int min: 80 max:\n400)} height: {466 (gen/int min: 80 max: 670)}) (rect position: [500\n500] colour: red width: {135 (gen/int min: 80 max: 400)} height: {603\n(gen/int min: 80 max: 670)}) (rect position: [500 500] colour: red\nwidth: {192 (gen/int min: 80 max: 400)} height: {624 (gen/int min: 80\nmax: 670)})",
);

seeded_unparse_check(
764,
"{b (gen/select from: '(a b c))}",
"{a (gen/select from: '(a b c))}",
"{b (gen/select from: '(a b c))}",
);

seeded_unparse_check(
764,
"{(col/rgb r: 1 g: 0 b: 0.4 alpha: 1) (gen/col)}",
"{(col/rgb r: 0.82 g: 0.65 b: 0.99 alpha: 0.26) (gen/col)}",
"{(col/rgb r: 0.65 g: 0.17 b: 0.89 alpha: 0.45) (gen/col)}",
);

seeded_unparse_check(
653,
"{(col/rgb r: 1 g: 0 b: 0.4 alpha: 1) (gen/col alpha: 1)}",
"{(col/rgb r: 0.78 g: 0.97 b: 0.89 alpha: 1.00) (gen/col alpha: 1)}",
"{(col/rgb r: 0.75 g: 0.59 b: 0.33 alpha: 1.00) (gen/col alpha: 1)}",
);
}

@@ -479,25 +474,25 @@ max: 670)})",
seeded_unparse_check(
653,
"{[[1.00 2.00] [3.00 4.00]] (gen/2d)}",
"{[[0.78 0.97] [0.89 0.11]] (gen/2d)}",
"{[[0.75 0.59] [0.33 0.85]] (gen/2d)}",
);

seeded_unparse_check(
653,
"{[[ 1.00 2.00 ] [ 3.00 4.00 ]] (gen/2d)}",
"{[[ 0.78 0.97 ] [ 0.89 0.11 ]] (gen/2d)}",
"{[[ 0.75 0.59 ] [ 0.33 0.85 ]] (gen/2d)}",
);

seeded_unparse_check(
653,
"{[[10 20] [30 40]] (gen/2d min: 60 max: 70)}",
"{[[68 70] [69 61]] (gen/2d min: 60 max: 70)}",
"{[[67 66] [63 69]] (gen/2d min: 60 max: 70)}",
);

seeded_unparse_check(
653,
"{ [ [ 50.1 60.23 ] [ 70.456 80.7890 ]] (gen/2d min: 40 max: 90) }",
"{ [ [ 79.1 88.54 ] [ 84.577 45.4872 ]] (gen/2d min: 40 max: 90) }",
"{ [ [ 77.3 69.69 ] [ 56.680 82.6415 ]] (gen/2d min: 40 max: 90) }",
);
}

@@ -506,7 +501,7 @@ max: 670)})",
seeded_unparse_check(
764,
"{[10 20] (gen/stray-2d from: [10 20] by: [5 5])}",
"{[8 23] (gen/stray-2d from: [10 20] by: [5 5])}",
"{[10 22] (gen/stray-2d from: [10 20] by: [5 5])}",
);
}

@@ -515,7 +510,7 @@ max: 670)})",
seeded_unparse_check(
764,
"{[0.977 0.416 0.171] (gen/scalar)}",
"{[0.265 0.816 0.654] (gen/scalar)}",
"{[0.454 0.653 0.172] (gen/scalar)}",
);
}


+ 9
- 1
server/static/gallery.json View File

@@ -1,5 +1,13 @@
[
{"id":65,"name":"1918-dev-bitmap","image":"img/seni/blank.png"},
{"id":73,"name":"1918-dev-bitmap","image":"img/seni/blank.png"},
{"id":72,"name":"sketch/1922-skull-face","image":"img/seni/blank.png"},
{"id":71,"name":"sketch/1922-skull","image":"img/seni/blank.png"},
{"id":70,"name":"sketch/1921-yellow-skull-helmet","image":"img/seni/blank.png"},
{"id":69,"name":"sketch/1922-hand","image":"img/seni/blank.png"},
{"id":68,"name":"sketch/1922-pencil-skull","image":"img/seni/blank.png"},
{"id":67,"name":"sketch/1919-skull","image":"img/seni/blank.png"},
{"id":66,"name":"1918-skull","image":"img/seni/blank.png"},
{"id":65,"name":"1918-einstein","image":"img/seni/blank.png"},
{"id":64,"name":"1841-schotteresque","image":"img/seni/160c-schotter.png"},
{"id":63,"name":"1841-nib","image":"img/seni/1841-nib.png"},
{"id":62,"name":"1840-boxes","image":"img/seni/1840-boxes.png"},

Loading…
Cancel
Save