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

Merge pull request #382 from tpronk/fix_seedrandom

BF: Make seedrandom work again
This commit is contained in:
Alain Pitiot 2021-06-10 15:26:00 +02:00 committed by GitHub
commit b9aee09474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 17 deletions

View File

@ -80,10 +80,9 @@ export class TrialHandler extends PsychObject
this._addAttribute('nReps', nReps); this._addAttribute('nReps', nReps);
this._addAttribute('method', method); this._addAttribute('method', method);
this._addAttribute('extraInfo', extraInfo); this._addAttribute('extraInfo', extraInfo);
this._addAttribute('seed', seed);
this._addAttribute('name', name); this._addAttribute('name', name);
this._addAttribute('autoLog', autoLog); this._addAttribute('autoLog', autoLog);
this._addAttribute('seed', seed);
this._prepareTrialList(trialList); this._prepareTrialList(trialList);
// number of stimuli // number of stimuli
@ -257,6 +256,23 @@ export class TrialHandler extends PsychObject
return snapshot; return snapshot;
} }
/**
* Setter for the seed attribute.
*
* @param {boolean} newSeed - New value for seed
*/
setSeed(seed, log)
{
this._setAttribute('seed', seed, log);
if (typeof this.seed !== 'undefined')
{
this._randomNumberGenerator = seedrandom(this.seed);
}
else
{
this._randomNumberGenerator = seedrandom();
}
}
/** /**
* Setter for the finished attribute. * Setter for the finished attribute.
@ -616,16 +632,6 @@ export class TrialHandler extends PsychObject
// get an array of the indices of the elements of trialList : // get an array of the indices of the elements of trialList :
const indices = Array.from(this.trialList.keys()); 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) if (this.method === TrialHandler.Method.SEQUENTIAL)
{ {
this._trialSequence = Array(this.nReps).fill(indices); this._trialSequence = Array(this.nReps).fill(indices);
@ -638,7 +644,7 @@ export class TrialHandler extends PsychObject
this._trialSequence = []; this._trialSequence = [];
for (let i = 0; i < this.nReps; ++i) for (let i = 0; i < this.nReps; ++i)
{ {
this._trialSequence.push(util.shuffle(indices.slice())); this._trialSequence.push(util.shuffle(indices.slice(), this._randomNumberGenerator));
} }
} }
@ -652,7 +658,7 @@ export class TrialHandler extends PsychObject
} }
// shuffle the sequence: // shuffle the sequence:
util.shuffle(flatSequence); util.shuffle(flatSequence, this._randomNumberGenerator);
// reshape it into the trialSequence: // reshape it into the trialSequence:
this._trialSequence = []; this._trialSequence = [];

View File

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