# pr1001/MathPlus

`…nt to generate numbers between min and max. add demos/ to show how some of the functions can be used.`
1 parent 6ddab2c commit 63685b7577693091e8458e298e564dd88140ae7f BruceJillis committed Jan 23, 2011
Showing with 208 additions and 1 deletion.
2. +72 −0 demos/normal.html
3. +102 −0 demos/randint.html
4. +28 −1 mathplus.js
 @@ -10,6 +10,12 @@ Everything lives under the M object. Functions include: - factorial: Returns NaN for negative numbers. Thanks to @Kambfhase for passing along @nikic's significant speed-up! Recursion may be elegant but it's slow. - limit: Provides an approximation. Works in some cases with +/- Infinity. Needs some additional tweaking. - derivative: Given f and x, will provide an approximation of the derivative of function f at point x. Round the result to get integers for derivatives of common functions, e.g. of f(x) = 3*x. +- normal: Given mean and deviation returns random values from a normal distribution. +- randint: Given a min, and max returns a random integer in the range: min <= result <= max + +## Demos + +The demos/ subdirectory contains a few demos that demonstrate the uses of the various functions. ## Vectors
72 demos/normal.html
 @@ -0,0 +1,72 @@ + + + + + + + +
+ Parameters +
+ + , +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
102 demos/randint.html
 @@ -0,0 +1,102 @@ + + + + + +

JavaScript Math.random() Test

+ Testing from min to max. Running iterations tests. +
+ Parameters +
+ + +
+
+ + +
+
+ + +
+ +
+ + + +
+ M.randint() +
+
+ +
+ Ad Hoc: Math.round(Math.random() * (max - min) + min) +
+ note: Notice the values "fall-off" near the limits of the range. +
+
+ +
+ Ceil: Math.ceil(Math.random() * (max - min + 1) + min - 1) +
+ note: There is a minute possibility that this function will return (min-1). This is why floor is a better choice. +
+
+ +

Based on work by Andrew Penry: Make JavaScript Math.random() useful, JavaScript Math.random() Test

+ +
29 mathplus.js
 @@ -95,13 +95,40 @@ var M = { } // make sure rangeEnd > rangeStart - if (rangeEnd < rangeStart) { + if (rangeEnd < rangeStart) { var tmp = rangeEnd; rangeEnd = rangeStart; rangeStart = tmp; } return ((Math.random() * (rangeEnd - rangeStart)) + rangeStart); }, + // generate random integers: min <= result <= max + // see: Andrew Penry's http://www.shawnolson.net/a/789/make-javascript-mathrandom-useful.html + 'randint': function random(min, max) { + if (typeof min != "number" && !(min instanceof Number)) { + throw new RangeError("min must be a valid number >= 0") + } + if (typeof max != "number" && !(max instanceof Number)) { + throw new RangeError("max must be a valid number >= 0") + } + return Math.floor(Math.random()*(max-min+1)+min) + }, + // N(m,s) + // returns random number with normal (gaussian) distribution: mean=m, std dev=s using the box-muller transformation + // see: http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform + 'normal': function normal(mean, deviation) { + if (typeof mean != "number" && !(mean instanceof Number)) { + throw new RangeError("mean must be a valid number") + } + if (typeof deviation != "number" && !(deviation instanceof Number)) { + throw new RangeError("deviation must be a valid number > 0") + } + var x = this.random(0, 1); + var y = this.random(0, 1); + var z1 = Math.sqrt(-2*Math.log(x))*Math.cos(2*Math.PI*y); + // var z2 = Math.sqrt(-2*Math.log(x))*Math.sin(2*Math.PI*y); + return (z1 * deviation) + mean; + }, // NOTE: The number is always converted to an integer 'factorial': function factorial(num) { num = parseInt(num)