-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandomizer.js
48 lines (41 loc) · 921 Bytes
/
randomizer.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
Randomizer = function(attrib) {
this.givers = {};
for (var key in attrib) {
this.givers[key] = new GiveItems(attrib[key]['items'], attrib[key]['repeats']);
}
this.next = function(key) {
//console.log(key);
//console.log(this.givers);
value = this.givers[key].next();
return value;
}
};
GiveItems = function(items, maxRepeats) {
this.items = items;
this.len = items.length;
this.maxRepeats = maxRepeats;
this.last = false;
this.repeats = 0;
this.next = function () {
//select an item
i = Math.floor((Math.random()*this.len)+1);
item = this.items[i-1];
//see if it repeats
if (item == this.last) {
this.repeats += 1;
}
else {
this.repeats = 0;
}
//now check to see if it's acceptable
if (this.repeats < this.maxRepeats) {
this.last = item;
}
else {
this.repeats -= 1;
item = this.next();
this.last = item;
}
return(this.last);
};
};