refactor: random

This commit is contained in:
cha0s 2022-07-05 10:08:16 -05:00
parent 05cad707bb
commit 0f5c262fae
10 changed files with 39 additions and 22 deletions

View File

@ -34,7 +34,8 @@
"@avocado/react": "^3.0.0", "@avocado/react": "^3.0.0",
"@flecks/core": "^1.4.1", "@flecks/core": "^1.4.1",
"@flecks/react": "^1.4.1", "@flecks/react": "^1.4.1",
"graham_scan": "^1.0.4" "graham_scan": "^1.0.4",
"seedrandom": "^3.0.5"
}, },
"devDependencies": { "devDependencies": {
"@flecks/fleck": "^1.4.1" "@flecks/fleck": "^1.4.1"

View File

@ -9,6 +9,7 @@ export * from './math';
export {default as floodwalk2D} from './floodwalk'; export {default as floodwalk2D} from './floodwalk';
export {default as QuadTree} from './quadtree'; export {default as QuadTree} from './quadtree';
export {noise, noiseSeed} from './noise'; export {noise, noiseSeed} from './noise';
export * from './random';
export {default as Range} from './range'; export {default as Range} from './range';
export {default as SimpleMovingAverage} from './sma'; export {default as SimpleMovingAverage} from './sma';
export * as Vertice from './vertice'; export * as Vertice from './vertice';

View File

@ -24,7 +24,6 @@ export const {
log10, log10,
max, max,
min, min,
random,
round, round,
sign, sign,
sin, sin,
@ -79,22 +78,8 @@ export const normalizeAngleRange = (min, max) => {
return [min, max]; return [min, max];
/* eslint-enable no-param-reassign */ /* eslint-enable no-param-reassign */
}; };
export const randomNumber = (min, max) => {
if (min > max) {
// eslint-disable-next-line no-param-reassign
[max, min] = [min, max];
}
return min + Math.random() * (max - min);
};
// smooth(er)?step assumes unit // smooth(er)?step assumes unit
export const smoothstep = (x) => x * x * (3 - 2 * x); export const smoothstep = (x) => x * x * (3 - 2 * x);
export const smootherstep = (x) => x * x * x * (x * (x * 6 - 15) + 10); export const smootherstep = (x) => x * x * x * (x * (x * 6 - 15) + 10);
/**
* Probabilistic behavior. Every `period` seconds, return `true`.
*
* @param {Number} elapsed The time that's passed.
* @param {Number} period The frequency of probabilistic behavior.
*/
export const spontaneous = (elapsed, period) => Math.random() < 1 / (period / elapsed);
export const sub = (l, r) => l - r; export const sub = (l, r) => l - r;
export const toRad = (a) => a * PI_180; export const toRad = (a) => a * PI_180;

View File

@ -0,0 +1,27 @@
const {alea: Alea} = require('seedrandom');
let generator = new Alea(0);
export function setSeed(seed) {
generator = new Alea(seed);
}
export function random() {
return generator();
}
export const randomNumber = (min, max) => {
if (min > max) {
// eslint-disable-next-line no-param-reassign
[max, min] = [min, max];
}
return min + random() * (max - min);
};
/**
* Probabilistic behavior. Every `period` seconds, return `true`.
*
* @param {Number} elapsed The time that's passed.
* @param {Number} period The frequency of probabilistic behavior.
*/
export const spontaneous = (elapsed, period) => random() < 1 / (period / elapsed);

View File

@ -1,4 +1,4 @@
import {randomNumber} from './math'; import {randomNumber} from './random';
export default class Range { export default class Range {

View File

@ -1,5 +1,5 @@
import Range from '../range'; import Range from '../range';
import {randomNumber} from '../math'; import {randomNumber} from '../random';
export default class VectorRange extends Range { export default class VectorRange extends Range {

View File

@ -30,6 +30,7 @@
"test.js.map" "test.js.map"
], ],
"dependencies": { "dependencies": {
"@avocado/math": "^3.0.0",
"@avocado/react": "^3.0.0", "@avocado/react": "^3.0.0",
"@avocado/resource": "^3.0.0", "@avocado/resource": "^3.0.0",
"@avocado/traits": "^3.0.0", "@avocado/traits": "^3.0.0",

View File

@ -1,3 +1,4 @@
import {random} from '@avocado/math';
import {JsonResource, Resource} from '@avocado/resource'; import {JsonResource, Resource} from '@avocado/resource';
import LRU from 'lru-cache'; import LRU from 'lru-cache';
@ -37,7 +38,7 @@ const pull = () => {
} }
} }
const hmi = MAX_INTERVAL / 2; const hmi = MAX_INTERVAL / 2;
setTimeout(pull, Math.max(20, hmi + hmi * (Math.random() - 0.5))); setTimeout(pull, Math.max(20, hmi + hmi * (random() - 0.5)));
}; };
if ('web' === process.env.FLECKS_CORE_BUILD_TARGET) { if ('web' === process.env.FLECKS_CORE_BUILD_TARGET) {

View File

@ -1,4 +1,5 @@
import {Property} from '@avocado/core'; import {Property} from '@avocado/core';
import {random} from '@avocado/math';
import {Class, compose, EventEmitter} from '@flecks/core'; import {Class, compose, EventEmitter} from '@flecks/core';
import Transition from '../transition'; import Transition from '../transition';
@ -10,7 +11,7 @@ const Modulator = {
}, },
Random() { Random() {
return () => Math.random(); return () => random();
}, },
Sawtooth() { Sawtooth() {

View File

@ -1,6 +1,6 @@
import {mapValuesAsync} from '@avocado/core'; import {mapValuesAsync} from '@avocado/core';
import {StateProperty, Trait} from '@avocado/traits'; import {StateProperty, Trait} from '@avocado/traits';
import {Rectangle, Vector} from '@avocado/math'; import {random, Rectangle, Vector} from '@avocado/math';
import {compose} from '@flecks/core'; import {compose} from '@flecks/core';
import mapValues from 'lodash.mapvalues'; import mapValues from 'lodash.mapvalues';
@ -282,7 +282,7 @@ export default (flecks) => class Animated extends decorate(Trait) {
const animation = this.$$animations[currentAnimation]; const animation = this.$$animations[currentAnimation];
const jitterAmount = this.jitterFor(currentAnimation); const jitterAmount = this.jitterFor(currentAnimation);
if (jitterAmount > 0) { if (jitterAmount > 0) {
const jitter = Math.random() * jitterAmount; const jitter = random() * jitterAmount;
const halfJitter = jitter / 2; const halfJitter = jitter / 2;
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
elapsed += jitter - halfJitter; elapsed += jitter - halfJitter;