-
Notifications
You must be signed in to change notification settings - Fork 7
/
randName.js
executable file
·33 lines (25 loc) · 1.03 KB
/
randName.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
// Random name generation
// Takes in input an array of arrays of strings. When generating a random name,
// picks a string at random from each array and sticks them together. It can
// also be asked to generate a precise string with given chunk indices
var RandName = function ( chunks ) {
this.chunks = chunks;
// Returns a totally random name
this.rnd = function ( ) {
var result = "";
for ( var i = 0; i < this.chunks.length; ++i )
result += this.chunks[i][Math.floor(Math.random() * this.chunks[i].length)];
// Capitalize first letter
result = result.charAt(0).toUpperCase() + result.substr(1);
return result;
}
// Returns a name obtained by merging chunks corresponding to given indices
this.get = function ( idx ) {
var result = "";
for ( var i = 0; i < this.chunks.length; ++i )
result += this.chunks[i][idx[i] % this.chunks[i].length];
// Capitalize first letter
result = result.charAt(0).toUpperCase() + result.substr(1);
return result;
}
}