Skip to content

Commit

Permalink
fix: random walk
Browse files Browse the repository at this point in the history
simplify the function
  • Loading branch information
201flaviosilva committed May 30, 2023
1 parent 905294c commit c137f75
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 313 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"author": "201flaviosilva",
"license": "MIT",
"type": "module",
"main": "./dist/counter.umd.cjs",
"module": "./dist/counter.js",
"main": "./dist/utilidades.umd.cjs",
"module": "./dist/utilidades.js",
"types": "./types/main.d.ts",
"scripts": {
"clear": "rm -rf types && rm -rf docs && rm -rf build && rm -rf dist",
Expand Down Expand Up @@ -40,8 +40,8 @@
],
"exports": {
"types": "./types/main.d.ts",
"import": "./dist/counter.js",
"require": "./dist/counter.umd.cjs"
"import": "./dist/utilidades.js",
"require": "./dist/utilidades.umd.cjs"
},
"keywords": [
"utils"
Expand Down
14 changes: 0 additions & 14 deletions snippets/BasicNodeWebSocketSetup/index.js

This file was deleted.

31 changes: 0 additions & 31 deletions snippets/DocusaurusPlugins/Readme.md

This file was deleted.

31 changes: 0 additions & 31 deletions snippets/DocusaurusPlugins/copy2docusaurus.js

This file was deleted.

61 changes: 0 additions & 61 deletions snippets/DocusaurusPlugins/jsdoc2docusaurus.js

This file was deleted.

41 changes: 0 additions & 41 deletions snippets/Jest/MockStorage.js

This file was deleted.

53 changes: 0 additions & 53 deletions snippets/NinjaDom/NinjaDom.js

This file was deleted.

23 changes: 0 additions & 23 deletions snippets/Singleton/Singleton.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { and, or, xor } from "./logicalOperators.js";
import { randomColor, randomColor0X, randomRGBAColor, randomRGBColor } from "./randomColor.js";
import { randomFloat, randomInt, randomNumber } from "./randomNumber.js";
import { randomString } from "./randomString.js";
import { randomWalk1D, randomWalk2D, randomWalk3D } from "./randomWalk.js";
import { randomWalk } from "./randomWalk.js";
import { reverseString } from "./reverseString.js";

export {
Expand Down Expand Up @@ -51,7 +51,7 @@ export {
randomColor, randomColor0X, randomRGBColor, randomRGBAColor,
randomNumber, randomInt, randomFloat,
randomString,
randomWalk1D, randomWalk2D, randomWalk3D,
randomWalk,
reverseString,
};

62 changes: 14 additions & 48 deletions src/randomWalk.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,21 @@
/**
* Returns a random value -1 or 1
* Perform a random walk in two dimensions.
*
* @see {@link https://pt.wikipedia.org/wiki/Passeio_aleat%C3%B3rio}
*
* @returns {number[]}
*/
export function randomWalk1D() {
return [Math.random() < 0.5 ? -1 : 1];
}

/**
* Return a changed random axis (x or y)
*
* @see {@link https://pt.wikipedia.org/wiki/Passeio_aleat%C3%B3rio}
*
* @returns {number[]}
*/
export function randomWalk2D() {
if (Math.random() < 0.5) return [...randomWalk1D(), 0];
else return [0, ...randomWalk1D()];
}

/**
* Return a changed random axis (x, y or z)
*
* @see {@link https://pt.wikipedia.org/wiki/Passeio_aleat%C3%B3rio}
*
* @returns {number[]}
* @param {number} steps - The number of steps to take in the random walk.
* @returns {{x: number, y: number}} - The final position after the random walk, with x and y coordinates.
*/
export function randomWalk3D() {
const rdn = Math.random();
if (rdn < 0.333) return [...randomWalk1D(), 0, 0];
if (rdn < 0.666) return [0, ...randomWalk1D(), 0];
else return [0, 0, ...randomWalk1D()];
}
export function randomWalk(steps) {
let x = 0;
let y = 0;

for (let i = 0; i < steps; i++) {
const random = Math.random();

// TODO: Update this function
export function randomWalk(
numberOfAxes = 2,
trend = {
left: 0.25, right: 0.25,
up: 0.25, down: 0.25,
front: 0, backward: 0,
},
bounds = {
x: { min: -Infinity, max: Infinity, },
y: { min: -Infinity, max: Infinity, },
z: { min: -Infinity, max: Infinity, },
}) {

if (numberOfAxes === 3) {
if (random < 0.25) x++;
else if (random < 0.5) x--;
else if (random < 0.75) y++;
else y--;
}

return { x, y };
}
1 change: 1 addition & 0 deletions tests/Arrays/choice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { choice } = Arrays;

describe("Arrays/choice.js", () => {
let spy;

beforeEach(() => {
spy = vitest.spyOn(global.Math, "random").mockReturnValue(0.1);
});
Expand Down

0 comments on commit c137f75

Please sign in to comment.