Skip to content

Commit c8c8141

Browse files
refactor: update .probability() call sites in various pkgs
1 parent efdd49c commit c8c8141

File tree

10 files changed

+18
-16
lines changed

10 files changed

+18
-16
lines changed

packages/associative/src/sorted-map.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class SortedMap<K, V> extends Map<K, V> {
213213
this.insertInLane(node, newNode);
214214
let currLevel = node.level;
215215
let headLevel = $this.head.level;
216-
while (rnd.float() < p) {
216+
while (rnd.probability(p)) {
217217
// check if new head (at a new level) is needed
218218
if (currLevel >= headLevel) {
219219
const newHead = new Node<K, V>(

packages/cellular/src/1d.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class MultiCA1D implements IClear {
254254
const fn =
255255
target === "prob" ? () => rnd.float() : () => rnd.int() % num;
256256
for (let x = 0, width = this.width; x < width; x++) {
257-
if (rnd.float() < prob) dest[x] = fn();
257+
if (rnd.probability(prob)) dest[x] = fn();
258258
}
259259
return this;
260260
}
@@ -284,10 +284,9 @@ export class MultiCA1D implements IClear {
284284
const { width, prob, gens, configs, mask } = this;
285285
const [next, curr] = gens;
286286
for (let x = 0; x < width; x++) {
287-
next[x] =
288-
rnd.float() < prob[x]
289-
? this.computeCell(configs[mask[x]], x, curr[x])
290-
: curr[x];
287+
next[x] = rnd.probability(prob[x])
288+
? this.computeCell(configs[mask[x]], x, curr[x])
289+
: curr[x];
291290
}
292291
gens.unshift(gens.pop()!);
293292
}
@@ -356,7 +355,7 @@ export class MultiCA1D implements IClear {
356355
const $ = (id: Target, conf?: Partial<UpdateBufferOpts>) => {
357356
conf &&
358357
conf.perturb &&
359-
rnd.float() < conf.perturb &&
358+
rnd.probability(conf.perturb) &&
360359
this.setNoise(id, conf.density || 0.05, rnd);
361360
};
362361
for (let y = 0; y < height; y++) {

packages/dcons/src/dcons.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class DCons<T>
291291
let cell = this._head;
292292
while (cell) {
293293
const next = cell.next;
294-
rnd.float() < 0.5 ? this.asHead(cell) : this.asTail(cell);
294+
rnd.probability(0.5) ? this.asHead(cell) : this.asTail(cell);
295295
cell = next;
296296
}
297297
}

packages/gp/src/ast.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class AST<OP, T> {
7171
if (!loc) return tree;
7272
while (true) {
7373
let nextLoc: Location<ASTNode<OP, T>> | undefined;
74-
if (rnd!.float() < probMutate) {
74+
if (rnd!.probability(probMutate)) {
7575
loc = loc.replace(
7676
this.randomASTNode(0, Math.min(limit - loc.depth, maxDepth))
7777
);

packages/gp/src/mep.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class MEP<OP, T> {
9494
const res: MEPChromosome<OP, T> = [];
9595
const minLen = Math.min(chromo1.length, chromo2.length);
9696
for (let i = 0; i < minLen; i++) {
97-
res[i] = rnd.float() < 0.5 ? chromo1[i] : chromo2[i];
97+
res[i] = rnd.probability(0.5) ? chromo1[i] : chromo2[i];
9898
}
9999
return chromo1.length > minLen
100100
? res.concat(chromo1.slice(minLen))
@@ -107,7 +107,9 @@ export class MEP<OP, T> {
107107
const { rnd, probMutate } = this.opts;
108108
const res: MEPChromosome<OP, T> = new Array(chromo.length);
109109
for (let i = chromo.length; i-- > 0; ) {
110-
res[i] = rnd!.float() < probMutate ? this.randomGene(i) : chromo[i];
110+
res[i] = rnd!.probability(probMutate)
111+
? this.randomGene(i)
112+
: chromo[i];
111113
}
112114
return res;
113115
}

packages/lsys/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const TURTLE_IMPL_2D: RuleImplementations<Turtle2D> = {
113113
"/": (ctx) =>
114114
ctx.alive && (ctx.theta += ctx.rnd.norm(ctx.delta * ctx.jitter)),
115115
// kill branch (stochastic)
116-
k: (ctx) => ctx.alive && (ctx.alive = ctx.rnd.float() < ctx.aliveProb),
116+
k: (ctx) => ctx.alive && (ctx.alive = ctx.rnd.probability(ctx.aliveProb)),
117117
// decay alive probability
118118
p: (ctx) => ctx.alive && (ctx.aliveProb *= ctx.decayAlive),
119119
// grow alive probability

packages/random-fxhash/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export const RND = new SFC32(seed);
6464
* @param p
6565
* @param rnd - default {@link RND}
6666
*/
67-
export const probability = (p: number, rnd: IRandom = RND) => rnd.float() < p;
67+
export const probability = (p: number, rnd: IRandom = RND) =>
68+
rnd.probability(p);
6869

6970
/**
7071
* Wrapper for {@link @thi.ng/random#pickRandom}. Returns a random item from

packages/rasterize/src/shader.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const defNoise = <T = number>(
5454
rnd: SYSTEM,
5555
...opts,
5656
};
57-
return () => (rnd.float() < probability ? a : b);
57+
return () => (rnd.probability(probability) ? a : b);
5858
};
5959

6060
/**

packages/transducers-binary/src/random-bits.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ export const randomBits = (
2020
num?: number,
2121
rnd: IRandom = SYSTEM
2222
): IterableIterator<number> =>
23-
repeatedly(() => (rnd.float() < prob ? 1 : 0), num);
23+
repeatedly(() => (rnd.probability(prob) ? 1 : 0), num);

packages/transducers/src/sample.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function sample<T>(...args: any[]): any {
3737
return (rfn: Reducer<any, T>) => {
3838
const r = rfn[2];
3939
return compR(rfn, (acc, x: T) =>
40-
rnd.float() < prob ? r(acc, x) : acc
40+
rnd.probability(prob) ? r(acc, x) : acc
4141
);
4242
};
4343
}

0 commit comments

Comments
 (0)