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",
"@flecks/core": "^1.4.1",
"@flecks/react": "^1.4.1",
"graham_scan": "^1.0.4"
"graham_scan": "^1.0.4",
"seedrandom": "^3.0.5"
},
"devDependencies": {
"@flecks/fleck": "^1.4.1"

View File

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

View File

@ -24,7 +24,6 @@ export const {
log10,
max,
min,
random,
round,
sign,
sin,
@ -79,22 +78,8 @@ export const normalizeAngleRange = (min, max) => {
return [min, max];
/* 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
export const smoothstep = (x) => x * x * (3 - 2 * x);
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 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 {

View File

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

View File

@ -30,6 +30,7 @@
"test.js.map"
],
"dependencies": {
"@avocado/math": "^3.0.0",
"@avocado/react": "^3.0.0",
"@avocado/resource": "^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 LRU from 'lru-cache';
@ -37,7 +38,7 @@ const pull = () => {
}
}
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) {

View File

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

View File

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