-function generateRandomInteger (max, min = 0) {
- if (min) {
- return Math.floor(Math.random() * (max - min + 1) + min)
+const crypto = require('crypto')
+
+/**
+ * Generate a cryptographically secure random number in the [0,1[ range
+ *
+ * @returns
+ */
+function secureRandom () {
+ return crypto.randomBytes(4).readUInt32LE() / 0x100000000
+}
+
+/**
+ * Generate a cryptographically secure random number in the [0,1[ range
+ *
+ * @returns
+ */
+function secureRandomWithRandomValues () {
+ return crypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000
+}
+
+/**
+ * @param max
+ * @param min
+ * @returns
+ */
+function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
+ if (max < min || max < 0 || min < 0) {
+ throw new RangeError('Invalid interval')
+ }
+ max = Math.floor(max)
+ if (min != null && min !== 0) {
+ min = Math.ceil(min)
+ return Math.floor(secureRandom() * (max - min + 1)) + min
+ }
+ return Math.floor(secureRandom() * (max + 1))
+}
+
+/**
+ *
+ * @param max
+ * @param min
+ * @returns
+ */
+function generateRandomFloat (max = Number.MAX_VALUE, min = 0) {
+ if (max < min) {
+ throw new RangeError('Invalid interval')