spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[RANDBETWEEN] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-05-25 03:23:27
stats
7 file(s) changed, 86 insertions(+), 3 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Math.js
src/Formulas/AllFormulas.ts
src/Formulas/Math.ts
tests/Formulas/MathTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index f118041..9a675a4 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -916,6 +916,16 @@
  6 @constructor
  7 ```
  8 
  9+### NDBETWEEN 
 10+
 11+```
 12+  Returns a uniformly random integer between two values, inclusive on high and low. Values with decimal parts may be used for low andor high; this will cause the least and greatest possible values to be the next integer greater than low andor the next integer less than high, respectively. 
 13+@param low - lowest value 
 14+@param high - highest value 
 15+@returns {number} between low and high. 
 16+@constructor
 17+```
 18+
 19 ### TRUNC 
 20 
 21 ```
 22diff --git a/TODO.md b/TODO.md
 23index 9de0c54..ba4e331 100644
 24--- a/TODO.md
 25+++ b/TODO.md
 26@@ -54,7 +54,6 @@ For example 64 tbs to a qt.
 27 * MULTINOMIAL
 28 * PRODUCT
 29 * QUOTIENT
 30-* RANDBETWEEN
 31 * SERIESSUM
 32 * SIGN
 33 * SUBTOTAL
 34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 35index c476a0c..9621939 100644
 36--- a/dist/Formulas/AllFormulas.js
 37+++ b/dist/Formulas/AllFormulas.js
 38@@ -55,6 +55,7 @@ exports.COMBIN = Math_1.COMBIN;
 39 exports.MULTIPLY = Math_1.MULTIPLY;
 40 exports.MINUS = Math_1.MINUS;
 41 exports.RAND = Math_1.RAND;
 42+exports.RANDBETWEEN = Math_1.RANDBETWEEN;
 43 var Logical_1 = require("./Logical");
 44 exports.AND = Logical_1.AND;
 45 exports.EXACT = Logical_1.EXACT;
 46diff --git a/dist/Formulas/Math.js b/dist/Formulas/Math.js
 47index c7c815e..63430a0 100644
 48--- a/dist/Formulas/Math.js
 49+++ b/dist/Formulas/Math.js
 50@@ -778,6 +778,27 @@ var RAND = function () {
 51     return Math.random();
 52 };
 53 exports.RAND = RAND;
 54+/**
 55+ * Returns a uniformly random integer between two values, inclusive on high and low. Values with decimal parts may be
 56+ * used for low and/or high; this will cause the least and greatest possible values to be the next integer greater than
 57+ * low and/or the next integer less than high, respectively.
 58+ * @param low - lowest value
 59+ * @param high - highest value
 60+ * @returns {number} between low and high.
 61+ * @constructor
 62+ */
 63+var RANDBETWEEN = function (low, high) {
 64+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "RAND");
 65+    low = Math.floor(TypeConverter_1.TypeConverter.firstValueAsNumber(low));
 66+    high = Math.ceil(TypeConverter_1.TypeConverter.firstValueAsNumber(high));
 67+    if (low > high) {
 68+        throw new Errors_1.NumError("Function RANDBETWEEN parameter 2 value is " + low + ". It should be greater than or equal to "
 69+            + high + ".");
 70+    }
 71+    var diff = Math.abs(low - high);
 72+    return Math.round(low + (Math.random() * diff));
 73+};
 74+exports.RANDBETWEEN = RANDBETWEEN;
 75 /**
 76  * Truncates a number to a certain number of significant digits by omitting less significant digits.
 77  * @param value - The value to be truncated.
 78diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 79index 79602c9..f8bc6c9 100644
 80--- a/src/Formulas/AllFormulas.ts
 81+++ b/src/Formulas/AllFormulas.ts
 82@@ -52,7 +52,8 @@ import {
 83   COMBIN,
 84   MULTIPLY,
 85   MINUS,
 86-  RAND
 87+  RAND,
 88+  RANDBETWEEN
 89 } from "./Math";
 90 import {
 91   AND,
 92@@ -273,5 +274,6 @@ export {
 93   WORKDAY$INTL,
 94   MULTIPLY,
 95   MINUS,
 96-  RAND
 97+  RAND,
 98+  RANDBETWEEN
 99 }
100\ No newline at end of file
101diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
102index f96a998..8fb48c2 100644
103--- a/src/Formulas/Math.ts
104+++ b/src/Formulas/Math.ts
105@@ -785,6 +785,29 @@ var RAND = function () {
106   return Math.random();
107 };
108 
109+
110+/**
111+ * Returns a uniformly random integer between two values, inclusive on high and low. Values with decimal parts may be
112+ * used for low and/or high; this will cause the least and greatest possible values to be the next integer greater than
113+ * low and/or the next integer less than high, respectively.
114+ * @param low - lowest value
115+ * @param high - highest value
116+ * @returns {number} between low and high.
117+ * @constructor
118+ */
119+var RANDBETWEEN = function (low, high) {
120+  ArgsChecker.checkLength(arguments, 2, "RAND");
121+  low = Math.floor(TypeConverter.firstValueAsNumber(low));
122+  high = Math.ceil(TypeConverter.firstValueAsNumber(high));
123+  if (low > high) {
124+    throw new NumError("Function RANDBETWEEN parameter 2 value is " + low + ". It should be greater than or equal to "
125+      + high + ".");
126+  }
127+  var diff = Math.abs(low - high);
128+  return Math.round(low + (Math.random() * diff));
129+};
130+
131+
132 /**
133  * Truncates a number to a certain number of significant digits by omitting less significant digits.
134  * @param value - The value to be truncated.
135@@ -1080,5 +1103,6 @@ export {
136   RADIANS,
137   DEGREES,
138   COMBIN,
139-  RAND
140+  RAND,
141+  RANDBETWEEN
142 }
143\ No newline at end of file
144diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
145index ae27e35..e79f5f2 100644
146--- a/tests/Formulas/MathTest.ts
147+++ b/tests/Formulas/MathTest.ts
148@@ -52,7 +52,7 @@ import {
149   DEGREES,
150   COMBIN,
151   MINUS,
152-  RAND
153+  RAND, RANDBETWEEN
154 } from "../../src/Formulas/Math";
155 import * as ERRORS from "../../src/Errors";
156 import {
157@@ -1183,8 +1183,27 @@ test("MINUS", function(){
158   }, ERRORS.NA_ERROR);
159 });
160 
161+
162 test("RAND", function(){
163   catchAndAssertEquals(function() {
164     RAND.apply(this, [3]);
165   }, ERRORS.NA_ERROR);
166 });
167+
168+
169+test("RANDBETWEEN", function(){
170+  for(var i = 0; i < 1000; i++) {
171+    var x = RANDBETWEEN(1, 4);
172+    assertEquals(x >= 1, true);
173+    assertEquals(x <= 4, true);
174+    x = RANDBETWEEN(-1, 1);
175+    assertEquals(x >= -1, true);
176+    assertEquals(x <= 1, true);
177+  }
178+  catchAndAssertEquals(function() {
179+    RANDBETWEEN.apply(this, [3]);
180+  }, ERRORS.NA_ERROR);
181+  catchAndAssertEquals(function() {
182+    RANDBETWEEN.apply(this, [3, 4, 5]);
183+  }, ERRORS.NA_ERROR);
184+});