-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.js
202 lines (153 loc) · 5.49 KB
/
random.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Random Class:
A javascript class that has functions
for generating pseudorandom numbers.
Eg.:
const random = new Random();
List of class methods below:
Copyright 2022. Eduardo Programador
www.eduardoprogramador.com
All Rights Reserved
*/
class Random {
/*
Returns a random integer from a given range.
Parameters:
low: the smallest integer (inclusive)
high: the highest integer (inclusive)
Eg.: const random = new Random();
var some = random.get(1,50);
(returns a integer between 1 and 50)
(output) 27
*/
get(low, high) {
const rand = Math.random();
return Math.floor(high * rand) + 1;
}
/*
Returns a list of random numbers from a given
range and size.
Parameters:
size: The total of numbers to be generated
low: the smallest integer
high: the highest integer
Eg.: const random = new Random();
let list = random.getList(5,1,10);
(returns five numbers from 1 to 10)
(output) 1,2,4,6,9, etc...
*/
getList(size, low, high) {
var list = [];
for(let i = 0; i < size; i++) {
const rand = Math.random();
list.push(Math.floor(high * rand) + 1);
}
return list.sort((a,b) => a-b);
}
/*
Returns a string of randon characters
including letters of alphabet and numbers from 0 to 9.
Parameters:
size: The total number of characters to be generated.
Eg.: const random = new Random();
random.withNoSpecial(20);
(returns a list of characters).
(output) 49jzb82cg437gmb5kqhb ..etc...
*/
withNoSpecial(size) {
var res = ""
var isUpper = false;
const alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','y','z'];
const numbers = [0,1,2,3,4,5,6,7,8,9];
for(let i = 0; i < size; i++) {
const letter = this.get(1,2);
switch (letter) {
case 1:
isUpper = false;
break;
case 2:
isUpper = true;
break;
default:
isUpper = false;
break;
}
const type = this.get(1,2);
switch (type) {
case 1:
const element = this.get(0,alpha.length - 1);
const char = alpha[element];
res += char;
break;
case 2:
const el = this.get(0,numbers.length - 1);
const num = numbers[el];
res += num;
break;
default:
const ele = this.get(0,alpha.length - 1);
const charr = alpha[ele];
res += charr;
break;
}
}
return res;
}
/*
Returns a string of randon characters
including letters of alphabet and numbers from 0 to 9,
with addition of special characters.
Parameters:
size: The total number of characters to be generated.
Eg.: const random = new Random();
random.withSpecial(10);
(returns a list of characters).
(output) ki92v$#h1c ..etc...
*/
withSpecial(size) {
var res = ""
var isUpper = false;
const alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','y','z'];
const special = ['!','@','#','$','%','&','*','+'];
const numbers = [0,1,2,3,4,5,6,7,8,9];
for(let i = 0; i < size; i++) {
const letter = this.get(1,2);
switch (letter) {
case 1:
isUpper = false;
break;
case 2:
isUpper = true;
break;
default:
isUpper = false;
break;
}
const type = this.get(1,3);
switch (type) {
case 1:
const element = this.get(0,alpha.length - 1);
const char = alpha[element];
res += char;
break;
case 2:
const el = this.get(0,numbers.length - 1);
const num = numbers[el];
res += num;
break;
case 3:
const spec = this.get(0,special.length - 1);
const sp = special[spec];
res += sp;
break;
default:
const ele = this.get(0,alpha.length - 1);
const charr = alpha[ele];
res += charr;
break;
}
}
return res;
}
}