Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/*
test/*
30 changes: 30 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"extends": "airbnb-base",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"camelcase": "warn",
"indent": [
"error",
4
],
"no-param-reassign": ["error", { "props": false}],
"object-curly-newline": ["error", {
"ObjectExpression": "always",
"ObjectPattern": { "multiline": true },
"ImportDeclaration": "never",
"ExportDeclaration": { "multiline": true, "minProperties": 3 }
}],
"no-underscore-dangle": "off"
}
}
63 changes: 40 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
node-true-random is a true random "generator" written in ecmascript 6 (automatically compiles into es5 with traceur).
It caches random numbers from http://qrng.anu.edu.au/ for you to use.
node-true-random is a true random "generator" written in ecmascript 6.
It caches random numbers from http://qrng.anu.edu.au/ for you to use.

Why ecmascript 6 you might ask? Because classes are awesome!

It's very simple to use:

var random = require('true-random'); //Import the library
var gen = new random.true_rand(); //Create a number generator, this function accepts a cache_size, min_cache and a callback parameter
var integer = gen.integer() //returns a integer between 0 and 1, you can pass diffrent min and max values with integer(min, max)
var integer_array = gen.integers(0, 1, 10) //returns a array with 10 numbers between 0 and 1, the min, max and number of integers can be passed to this function.

Example 1:

//Display a random number between 1 and 7
var random = require('true-random');
var gen = new random.true_rand(10, 5, function(gen) {
console.log(gen.integer(1,7));
});
Example 2:

//Display a random array of 25 numbers between 700 and 15000
var random = require('true-random');
var gen = new random.true_rand(100, 50, function(gen) {
console.log(gen.integers(700, 15000, 25))
});

```node
const { Random } = require('true-random'); //Import the library
const gen = new Random(); //Create a number generator, this function accepts a cache_size, min_cache and a debugging

//returns primise that resolves to an integer between 0 and 1, you can pass different min and max values with integer(min, max)
gen.integer()
.then((i) => console.log(i));

//returns a promise that resolves to an array with 10 numbers between 0 and 1, the min, max and number of integers can be passed to this function.
gen.integers(0, 1, 10)
.then((int_array) => console.log(int_array));
```


### Example 1:

```node

//Display a random number between 1 and 7
const { Random } = require('true-random');
const gen = new Random(10, 5);

gen.integer(1, 7)
.then((i) => console.log(i));
```

### Example 2:
```node

//Display a random array of 25 numbers between 700 and 15000
const { Random } = require('true-random');
const gen = new random.true_rand(100, 50);

gen.integers(700, 15000, 25)
.then((int_array) => console.log(int_array));
```

TODO:
* Cleanup code
* Cleanup code
133 changes: 0 additions & 133 deletions es6/index.js

This file was deleted.

76 changes: 72 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
// We need traceur to compile the ecmascript 6 code to ecmascript 5 so node can interpret it
traceur = require('traceur');
true_random = traceur.require(__dirname+'/es6/index.js');
module.exports = true_random;
const fetch = require('node-fetch');
const querystring = require('querystring');

class true_random {
constructor(cache_size = 100, min_cache = 50, debug = false) {
this.cache_size = cache_size;
this.cache = [];

this.debug = debug;
this.min_cache = min_cache;
}

_debug(s) {
if (this.debug === true) console.debug(s);
}

integer(min = 0, max = 1) {
return this._cache(1)
.then(() => (this.cache.shift() / 281474976710655) * (max - min) + min)
.catch((e) => {
this._debug(e);
return NaN;
});
}

integers(min = 0, max = 1, num = 1) {
return this._cache(num)
.then(() => this.cache.splice(0, num))
.then((nums) => nums.map((f) => (f / 281474976710655) * (max - min) + min))
.catch((e) => {
this._debug(e);
return NaN;
});
}

// returns a primise
_cache(numToGet) {
const num = this.cache_size;
const url = `http://qrng.anu.edu.au/API/jsonI.php?${querystring.stringify({
length: num, type: 'hex16', size: 6,
})}`;

const _this = this;

const cacheSize = this.cache.length;
const minCache = this.min_cache;

return new Promise((resolve, reject) => {
if (num > 1024 || num < 1) {
_this._debug(`Error num=${num}`);
return reject(new RangeError('Num argument is not in range (1, 1024)'));
}

return resolve(cacheSize - numToGet <= minCache || cacheSize < numToGet);
})
.then((needsToFetch) => {
if (!needsToFetch) return this.cache;

return fetch(url)
.then((res) => res.json())
.then((json) => json.data.map((s) => parseInt(s, 16)))
.then((nums) => {
this.cache = this.cache.concat(nums);
return this.cache;
});
});
}
}

module.exports = {
Random: true_random,
};
Loading