1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-12 08:38:10 +00:00

BF: Make seedrandom work again

This bug-fix consists of modifications to util.shuffle and TrialHandler:
1) util.shuffle supports an optional second argument, rng, which defaults to Math.random
2) TrialHandler now has a getter and setter for the seed attrbute. On setting this attribute it constructs a random number generator (rng) using seedrandom using the seed provided. This rng is passed to to util.shuffle when preparing a sequence with method RANDOM or FULL_RANDOM
This commit is contained in:
Thomas Pronk 2021-06-09 13:57:28 +01:00
parent 4e08d1e26f
commit 148dda46ff
2 changed files with 35 additions and 17 deletions

View File

@ -80,12 +80,11 @@ export class TrialHandler extends PsychObject
this._addAttribute('nReps', nReps);
this._addAttribute('method', method);
this._addAttribute('extraInfo', extraInfo);
this._addAttribute('seed', seed);
this._addAttribute('name', name);
this._addAttribute('autoLog', autoLog);
this.seed = seed;
this._prepareTrialList(trialList);
// number of stimuli
this.nStim = this.trialList.length;
@ -257,6 +256,31 @@ export class TrialHandler extends PsychObject
return snapshot;
}
/**
* Setter for the seed attribute.
*
* @param {boolean} newSeed - New value for seed
*/
set seed(newSeed)
{
this._seed = newSeed;
if (this._seed !== undefined)
{
this.rng = seedrandom(this._seed);
}
else
{
this.rng = seedrandom();
}
}
/**
* Getter for the seed attribute.
*/
get seed()
{
return this._seed;
}
/**
* Setter for the finished attribute.
@ -616,16 +640,6 @@ export class TrialHandler extends PsychObject
// get an array of the indices of the elements of trialList :
const indices = Array.from(this.trialList.keys());
// seed the random number generator:
if (typeof (this.seed) !== 'undefined')
{
seedrandom(this.seed);
}
else
{
seedrandom();
}
if (this.method === TrialHandler.Method.SEQUENTIAL)
{
this._trialSequence = Array(this.nReps).fill(indices);
@ -638,7 +652,7 @@ export class TrialHandler extends PsychObject
this._trialSequence = [];
for (let i = 0; i < this.nReps; ++i)
{
this._trialSequence.push(util.shuffle(indices.slice()));
this._trialSequence.push(util.shuffle(indices.slice(), this.rng));
}
}
@ -652,7 +666,7 @@ export class TrialHandler extends PsychObject
}
// shuffle the sequence:
util.shuffle(flatSequence);
util.shuffle(flatSequence, this.rng);
// reshape it into the trialSequence:
this._trialSequence = [];

View File

@ -340,13 +340,17 @@ export function IsPointInsidePolygon(point, vertices)
* @function
* @public
* @param {Object[]} array - the input 1-D array
* @param {Function} [rng = undefined] - A function used to generated random numbers in the interal [0, 1). Defaults to Math.random
* @return {Object[]} the shuffled array
*/
export function shuffle(array)
export function shuffle(array, rng = undefined)
{
if (rng === undefined) {
rng = Math.random;
}
for (let i = array.length - 1; i > 0; i--)
{
const j = Math.floor(Math.random() * (i + 1));
const j = Math.floor(rng() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;