Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"author": "Yuu6883",
"license": "MIT",
"devDependencies": {
"@swc-node/register": "^1.5.1",
"@swc/core": "^1.7.26",
"@types/ace": "^0.0.48",
"@types/file-saver": "^2.0.5",
"@types/react": "^18.0.14",
Expand Down Expand Up @@ -44,8 +46,6 @@
"webpack-dev-server": "^4.9.2"
},
"dependencies": {
"@swc-node/register": "^1.5.1",
"canvas": "^2.11.2",
"seedrandom": "^3.0.5"
"canvas": "^2.11.2"
}
}
8 changes: 4 additions & 4 deletions src/helpers/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PRNG } from "seedrandom";
import { Random } from "../random";

interface WritableArray<T> {
readonly length: number;
Expand Down Expand Up @@ -111,7 +111,7 @@ export class Helper {

public static shuffleFill<T extends WritableArray<number>>(
array: T,
rng: PRNG
rng: Random
) {
for (let i = 0; i < array.length; i++) {
const j = range(rng, i + 1);
Expand All @@ -120,7 +120,7 @@ export class Helper {
}
}

public static pick<E, T extends ArrayLike<E>>(array: T, rng: PRNG) {
public static pick<E, T extends ArrayLike<E>>(array: T, rng: Random) {
return array[range(rng, array.length)];
}

Expand Down Expand Up @@ -191,7 +191,7 @@ export class Helper {
}

// exclusive
export const range = (rng: PRNG, upper: number) => Math.floor(rng() * upper);
export const range = (rng: Random, upper: number) => Math.floor(rng.nextDouble() * upper);

export type vec3 = [number, number, number];
export type vec4 = [number, number, number, number];
6 changes: 3 additions & 3 deletions src/interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import seedrandom, { PRNG } from "seedrandom";
import { Random } from "./random";
import { Grid } from "./grid";
import { vec3 } from "./helpers/helper";
import { SymmetryHelper } from "./helpers/symmetry";
Expand All @@ -14,7 +14,7 @@ export class Interpreter {
public startgrid: Grid;

origin: boolean;
public rng: PRNG;
public rng: Random;
public time = 0;

public readonly changes: vec3[] = [];
Expand Down Expand Up @@ -62,7 +62,7 @@ export class Interpreter {
seed: number,
steps: number
): Generator<[Uint8Array, string, number, number, number]> {
this.rng = seedrandom(seed.toString());
this.rng = new Random(seed);
this.grid = this.startgrid;
this.grid.clear();

Expand Down
2 changes: 1 addition & 1 deletion src/mj-nodes/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class AllNode extends RuleNode {
firstHeuristic = heuristic;
firstHeuristicComputed = true;
}
const u = this.ip.rng.double();
const u = this.ip.rng.nextDouble();
list.push([
m,
this.temperature > 0
Expand Down
2 changes: 1 addition & 1 deletion src/mj-nodes/convchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class ConvChainNode extends Node {
continue;
}
if (temperature != 1) q = Math.pow(q, 1.0 / temperature);
if (q > ip.rng.double()) this.toggle(state, r);
if (q > ip.rng.nextDouble()) this.toggle(state, r);
}

this.counter++;
Expand Down
2 changes: 1 addition & 1 deletion src/mj-nodes/convolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class ConvolutionNode extends Node {
if (
input == rule.input &&
rule.output != grid.state[i] &&
(rule.p == 1.0 || this.ip.rng.double() < rule.p)
(rule.p == 1.0 || this.ip.rng.nextDouble() < rule.p)
) {
let success = true;
if (rule.sums != null) {
Expand Down
6 changes: 3 additions & 3 deletions src/mj-nodes/one.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PRNG } from "seedrandom";
import { Random } from "../random";
import { Field } from "../field";
import { Grid } from "../grid";
import { BoolArray2D } from "../helpers/datastructures";
Expand Down Expand Up @@ -61,7 +61,7 @@ export class OneNode extends RuleNode {
}
}

randomMatch(rng: PRNG): vec4 {
randomMatch(rng: Random): vec4 {
const { grid, matchMask, matches } = this;

if (this.potentials) {
Expand Down Expand Up @@ -112,7 +112,7 @@ export class OneNode extends RuleNode {
firstHeuristic = heuristic;
firstHeuristicComputed = true;
}
const u = rng.double();
const u = rng.nextDouble();
const key =
this.temperature > 0
? Math.pow(
Expand Down
7 changes: 4 additions & 3 deletions src/mj-nodes/overlap.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { alea } from "seedrandom";
import { Grid } from "../grid";
import { Loader } from "../loader";
import { Array2D } from "../helpers/datastructures";
import { Helper } from "../helpers/helper";
import { SymmetryHelper } from "../helpers/symmetry";

import { WFCNode } from ".";
import { Random } from "../random";

// A bit slower than C# (130ms vs 90ms, WaveFlower, ryzen 5800x)
export class OverlapNode extends WFCNode {
protected static state_rng = alea("", { entropy: true });
// protected static state_rng = alea("", { entropy: true });
protected static state_rng = new Random();

private patterns: Array2D<Uint8Array>;
private votes: Array2D<Uint32Array>;
Expand Down Expand Up @@ -245,7 +246,7 @@ export class OverlapNode extends WFCNode {
const offset = i * cols;

for (let c = 0; c < cols; c++) {
const value = buf[offset + c] + 0.1 * rng.double();
const value = buf[offset + c] + 0.1 * rng.nextDouble();
if (value > max) {
argmax = c;
max = value;
Expand Down
2 changes: 1 addition & 1 deletion src/mj-nodes/parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ParallelNode extends RuleNode {
const grid = this.grid;

const rule = this.rules[r];
if (ip.rng.double() > rule.p) return;
if (ip.rng.nextDouble() > rule.p) return;
this.last |= 1 << r;
rule.jit_apply_kernel(
grid.state,
Expand Down
10 changes: 5 additions & 5 deletions src/mj-nodes/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import seedrandom, { PRNG } from "seedrandom";
import { Random } from "../random";
import { Grid } from "../grid";
import { Helper, vec3, vec4 } from "../helpers/helper";

Expand Down Expand Up @@ -92,7 +92,7 @@ export class PathNode extends Node {
)
return RunState.FAIL;

const local = seedrandom(this.ip.rng.int32().toString());
const local = new Random(this.ip.rng.next());
let min = MX * MY * MZ,
max = -1;
let argmin: vec3 = [-1, -1, -1];
Expand All @@ -102,7 +102,7 @@ export class PathNode extends Node {
const g = generations[px + py * MX + pz * MX * MY];
if (g == -1) continue;
const dg = g;
const noise = 0.1 * local.double();
const noise = 0.1 * local.nextDouble();

if (dg + noise < min) {
min = dg + noise;
Expand Down Expand Up @@ -159,7 +159,7 @@ export class PathNode extends Node {
dy: number,
dz: number,
generations: Int32Array,
rng: PRNG
rng: Random
): vec3 {
const candidates: vec3[] = [];
const { grid, vertices, edges, inertia } = this;
Expand Down Expand Up @@ -217,7 +217,7 @@ export class PathNode extends Node {
if (inertia && (dx != 0 || dy != 0 || dz != 0)) {
let maxScalar = -4;
for (const [cx, cy, cz] of candidates) {
const noise = 0.1 * rng.double();
const noise = 0.1 * rng.nextDouble();
const cos =
(cx * dx + cy * dy + cz * dz) /
Math.sqrt(
Expand Down
4 changes: 2 additions & 2 deletions src/mj-nodes/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export abstract class RuleNode extends Node {
this instanceof AllNode,
this.limit,
this.depthCoefficient,
this.ip.rng.int32(),
this.ip.rng.next(),
true // viz
).run()
: Search.run(
Expand All @@ -296,7 +296,7 @@ export abstract class RuleNode extends Node {
this instanceof AllNode,
this.limit,
this.depthCoefficient,
this.ip.rng.int32(),
this.ip.rng.next(),
true // viz
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/mj-nodes/tile.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { alea } from "seedrandom";
import { Grid } from "../grid";
import { Loader } from "../loader";
import { Array2D, Array3Dflat, BoolArray3D } from "../helpers/datastructures";
import { Helper } from "../helpers/helper";
import { SymmetryHelper } from "../helpers/symmetry";

import { WFCNode } from ".";
import { Random } from "../random";

const FALSE = (_) => false;

export class TileNode extends WFCNode {
protected static state_rng = alea("", { entropy: true });
// protected static state_rng = alea("", { entropy: true });
protected static state_rng = new Random();

private tiledata: Uint8Array[];

Expand Down Expand Up @@ -480,7 +481,7 @@ export class TileNode extends WFCNode {
let max = -1.0;
let argmax = 0xff;
for (let c = 0; c < v.length; c++) {
const vote = v[c] + 0.1 * rng.double();
const vote = v[c] + 0.1 * rng.nextDouble();
if (vote > max) {
argmax = c;
max = vote;
Expand Down
18 changes: 9 additions & 9 deletions src/mj-nodes/wfc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import seedrandom, { PRNG } from "seedrandom";
import { Random } from "../random";
import { Grid } from "../grid";
import { Array3D, BoolArray2D } from "../helpers/datastructures";
import { Helper } from "../helpers/helper";
Expand Down Expand Up @@ -34,7 +34,7 @@ export abstract class WFCNode extends Branch {
public name: string;

private firstgo = true;
protected rng: PRNG;
protected rng: Random;

public override async load(
elem: Element,
Expand Down Expand Up @@ -121,7 +121,7 @@ export abstract class WFCNode extends Branch {
const goodseed = this.goodSeed();
if (goodseed === null) return RunState.FAIL;

this.rng = seedrandom(goodseed.toString());
this.rng = new Random(goodseed);
this.stacksize = 0;
this.wave.copyFrom(this.startwave, this.shannon);
this.firstgo = false;
Expand All @@ -147,8 +147,8 @@ export abstract class WFCNode extends Branch {
goodSeed(): number {
for (let k = 0; k < this.tries; k++) {
let obs = 0;
const seed = this.ip.rng.int32();
this.rng = seedrandom(seed.toString());
const seed = this.ip.rng.next();
this.rng = new Random(seed);
this.stacksize = 0;
this.wave.copyFrom(this.startwave, this.shannon);

Expand Down Expand Up @@ -178,7 +178,7 @@ export abstract class WFCNode extends Branch {
return null;
}

nextUnobservedNode(rng: PRNG) {
nextUnobservedNode(rng: Random) {
const { grid, wave, periodic, shannon } = this;
const { MX, MY, MZ } = grid;

Expand All @@ -197,7 +197,7 @@ export abstract class WFCNode extends Branch {
? wave.entropies[i]
: remainingValues;
if (remainingValues > 1 && entropy <= min) {
const noise = 1e-6 * rng.double();
const noise = 1e-6 * rng.nextDouble();
if (entropy + noise < min) {
min = entropy + noise;
argmin = i;
Expand All @@ -207,11 +207,11 @@ export abstract class WFCNode extends Branch {
return argmin;
}

observe(node: number, rng: PRNG) {
observe(node: number, rng: Random) {
const w = this.wave.data.row(node);
for (let t = 0; t < this.P; t++)
this.distribution[t] = w.get(t) ? this.weights[t] : 0;
const r = Helper.sampleWeights(this.distribution, rng.double());
const r = Helper.sampleWeights(this.distribution, rng.nextDouble());
for (let t = 0; t < this.P; t++)
if (w.get(t) !== (t === r)) this.ban(node, t);
}
Expand Down
6 changes: 3 additions & 3 deletions src/node/program.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import seedrandom from "seedrandom";
import { Random } from "./random";

import * as path from "path";
import * as fs from "fs";
Expand Down Expand Up @@ -45,7 +45,7 @@ export class Program {

public static palette: Map<string, Uint8ClampedArray> = new Map();

public static meta = seedrandom();
public static meta = new Random();

public static loadPalette() {
const ep = Loader.xmlParse(PaletteXML);
Expand Down Expand Up @@ -248,7 +248,7 @@ export class Model {
}

public randomize() {
this._seed = Math.abs(Program.meta.int32());
this._seed = Math.abs(Program.meta.next());
}

private scaleTime(t: number) {
Expand Down
Loading