-
Notifications
You must be signed in to change notification settings - Fork 1
/
chancer.js
126 lines (117 loc) · 3.51 KB
/
chancer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
var chancer = module.exports = {
random: random,
float: float,
int: int,
coinToss: coinToss,
fromArray: fromArray,
shuffleArray: shuffleArray,
fillArray: fillArray,
uuid: uuid
};
// Returns a floating-point number between 0 and 1
function random () {
return Math.random();
}
// Returns a random number between <min> (inclusive) and <max> (exclusive)
function float (min, max) {
if (typeof min === 'number' && typeof max === 'number') {
return Math.random() * (max - min) + min;
}
else {
return undefined;
}
}
// Returns a random integer between <min> (inclusive) and <max> (inclusive)
function int (min, max) {
if (typeof min === 'number' && typeof max === 'number') {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
else {
return undefined;
}
}
// Returns a 0 or 1 to represent a coin toss
// Optionally associate any other values for the coin sides (true/false, yes/no, heads/tails, etc)
function coinToss (heads, tails) {
var first = (typeof heads !== 'undefined') ? heads : 0;
var second = (typeof tails !== 'undefined') ? tails : 1;
return (Math.floor(Math.random() * 2) === 0) ? first : second;
}
// Returns a random item from an array
function fromArray (obj) {
if (typeof obj !== 'undefined' && obj && obj.constructor === Array) {
return obj[Math.floor(Math.random() * obj.length)];
}
else {
return undefined;
}
}
// Returns an array shuffled into a random order
function shuffleArray (obj) {
if (typeof obj !== 'undefined' && obj && obj.constructor === Array) {
return shuffle(obj);
}
else {
return undefined;
}
}
function shuffle (arr) {
for (var i = arr.length - 1; i >= 0; i--) {
var j = chancer.int(0, i);
/* istanbul ignore else */
if (j !== i) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
return arr;
}
// Returns an array of integers between <min> (inclusive) and <max> (inclusive)
// If no <total> specified return all possible values between <min> and <max>
function fillArray (min, max, total) {
if (typeof min === 'number' && typeof max === 'number') {
var num = (total) ? total : (max - min) + 1;
var arr;
/* istanbul ignore else */
if (typeof num === 'number' && (((max - min) + 1) >= num)) {
arr = generateArray(min, max, num);
}
return arr;
}
else {
return undefined;
}
}
function generateArray (min, max, total) {
var arr = [];
while (arr.length < total) {
var randomNumber = chancer.int(min, max);
var found = false;
for (var i = 0; i < arr.length; i++) {
/* istanbul ignore else */
if (arr[i] === randomNumber) {
found = true;
break;
}
}
/* istanbul ignore else */
if (!found) {
arr[arr.length] = randomNumber;
}
}
return arr;
}
// Returns a random value as a universally unique identifier (UUID) version 4 (RFC4122)
// xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
// where x is any hexadecimal digit and y is one of 8, 9, A, or B
// e.g., efe1f2aa-1e99-40f2-83fa-8519acd8c34c
function uuid () {
var template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
return template.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0;
var v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}