Browse Source

Added prng() method

WestLangley 6 years ago
parent
commit
d5ae07facc
3 changed files with 28 additions and 0 deletions
  1. 3 0
      docs/api/en/math/Math.html
  2. 5 0
      src/math/Math.d.ts
  3. 20 0
      src/math/Math.js

+ 3 - 0
docs/api/en/math/Math.html

@@ -83,6 +83,9 @@
 		<h3>[method:Integer randInt]( [param:Integer low], [param:Integer high] )</h3>
 		<h3>[method:Integer randInt]( [param:Integer low], [param:Integer high] )</h3>
 		<p>Random integer in the interval [page:Float low] to [page:Float high].</p>
 		<p>Random integer in the interval [page:Float low] to [page:Float high].</p>
 
 
+		<h3>[method:Float prng]( [param:Integer seed] )</h3>
+		<p>Deterministic pseudo-random float in the interval [ 0, 1 ]. The integer [page:Integer seed] is optional.</p>
+
 		<h3>[method:Float smoothstep]( [param:Float x], [param:Float min], [param:Float max] )</h3>
 		<h3>[method:Float smoothstep]( [param:Float x], [param:Float min], [param:Float max] )</h3>
 		<p>
 		<p>
 		[page:Float x] - The value to evaluate based on its position between min and max. <br />
 		[page:Float x] - The value to evaluate based on its position between min and max. <br />

+ 5 - 0
src/math/Math.d.ts

@@ -62,6 +62,11 @@ export namespace _Math {
    */
    */
 	export function randFloatSpread( range: number ): number;
 	export function randFloatSpread( range: number ): number;
 
 
+	/**
+   * Deterministic pseudo-random float in the interval [ 0, 1 ].
+   */
+	export function prng( seed?: number ): number;
+
 	export function degToRad( degrees: number ): number;
 	export function degToRad( degrees: number ): number;
 
 
 	export function radToDeg( radians: number ): number;
 	export function radToDeg( radians: number ): number;

+ 20 - 0
src/math/Math.js

@@ -117,6 +117,26 @@ var _Math = {
 
 
 	},
 	},
 
 
+	// Deterministic pseudo-random float in the interval [ 0, 1 ]
+
+	prng: function () {
+
+		var seed = 1234567;
+
+		return function prng( s ) {
+
+			if ( s !== undefined ) seed = s % 2147483647;
+
+			// Park-Miller algorithm
+
+			seed = seed * 16807 % 2147483647;
+
+			return ( seed - 1 ) / 2147483646;
+
+		};
+
+	}(),
+
 	degToRad: function ( degrees ) {
 	degToRad: function ( degrees ) {
 
 
 		return degrees * _Math.DEG2RAD;
 		return degrees * _Math.DEG2RAD;