-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomset.js
46 lines (38 loc) · 1.19 KB
/
randomset.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
'use strict';
var seedrandom = require('seedrandom');
module.exports = class RandomSet {
// Pick one of the 5 stimulus sets, with a stable ordering for each participant
// We're limiting to 5 now because the 6th was picked from to fix issues
// with the first 5
constructor(seedOrRng, order, lureDifficulty) {
if (typeof seedOrRng === "string" || typeof seedOrRng === "number") {
this.rng = seedrandom(seedOrRng);
} else {
this.rng = seedOrRng;
}
var stimulusSets = [1,2,3,4,5];
this.setOrder = this.shuffle(stimulusSets, this.rng);
}
// TODO: It would be good to unify this in a central place somehow instead of
// copying from order.js
shuffle(a, rng) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(rng() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
pickRandomStimulusSetForSession(session) {
var intSession = 0;
if (Number.isInteger(session)) {
intSession = parseInt(session);
} else {
var digits = session.toString().replace( /^\D+/g, '');
intSession = parseInt(digits);
}
return this.setOrder[intSession - 1];
}
}