mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-13 05:01:03 +00:00
Update rand.js
This commit is contained in:
parent
6f90f2f3d2
commit
f3a34f560d
26
lib/rand.js
26
lib/rand.js
|
@ -1,5 +1,7 @@
|
||||||
// rand.js
|
// rand.js
|
||||||
|
|
||||||
|
var FILE = require("lib/file");
|
||||||
|
|
||||||
function randomize() {
|
function randomize() {
|
||||||
return Math.random();
|
return Math.random();
|
||||||
}
|
}
|
||||||
|
@ -75,6 +77,26 @@ function sample(arr, length) {
|
||||||
return shuffle(arr).slice(0, length);
|
return shuffle(arr).slice(0, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function discardCallback(callback, filename) {
|
||||||
|
if (typeof callback !== "function") return;
|
||||||
|
|
||||||
|
var discarded = splitLn(FILE.readFile(filename, "utf-8"));
|
||||||
|
var chosen = callback();
|
||||||
|
|
||||||
|
while (chosen == null || discarded.indexOf(chosen) > -1) {
|
||||||
|
console.log("Chosen other item.");
|
||||||
|
chosen = callback(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE.appendFile(filename, "\r\n" + chosen, "utf-8");
|
||||||
|
|
||||||
|
return chosen;
|
||||||
|
}
|
||||||
|
|
||||||
|
function discardOne(arr, filename) {
|
||||||
|
return discardCallback(one, filename);
|
||||||
|
}
|
||||||
|
|
||||||
exports.randomize = randomize;
|
exports.randomize = randomize;
|
||||||
exports.getInt = getInt;
|
exports.getInt = getInt;
|
||||||
exports.one = one;
|
exports.one = one;
|
||||||
|
@ -83,7 +105,9 @@ exports.uuidv4 = uuidv4;
|
||||||
exports.shuffle = shuffle;
|
exports.shuffle = shuffle;
|
||||||
exports.rotate = rotate;
|
exports.rotate = rotate;
|
||||||
exports.sample = sample;
|
exports.sample = sample;
|
||||||
|
exports.discardOne = discardOne;
|
||||||
|
exports.discardCallback = discardCallback;
|
||||||
|
|
||||||
exports.VERSIONINFO = "Random Module (rand.js) version 0.4";
|
exports.VERSIONINFO = "Random Module (rand.js) version 0.5";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = global.require;
|
exports.require = global.require;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user