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

RF: leverage _setAttribute, rename rng to randomNumberGenerator

This commit is contained in:
Thomas Pronk 2021-06-09 16:04:57 +01:00
parent 6c292ea2d9
commit 170cf423d3
2 changed files with 12 additions and 12 deletions

View File

@ -261,16 +261,16 @@ export class TrialHandler extends PsychObject
*
* @param {boolean} newSeed - New value for seed
*/
setSeed(newSeed)
setSeed(newSeed, log)
{
this._seed = newSeed;
if (this._seed !== undefined)
this._setAttribute('seed', newSeed, log);
if (this.seed !== undefined)
{
this.rng = seedrandom(this._seed);
this._randomNumberGenerator = seedrandom(this.seed);
}
else
{
this.rng = seedrandom();
this._randomNumberGenerator = seedrandom();
}
}
@ -644,7 +644,7 @@ export class TrialHandler extends PsychObject
this._trialSequence = [];
for (let i = 0; i < this.nReps; ++i)
{
this._trialSequence.push(util.shuffle(indices.slice(), this.rng));
this._trialSequence.push(util.shuffle(indices.slice(), this._randomNumberGenerator));
}
}
@ -658,7 +658,7 @@ export class TrialHandler extends PsychObject
}
// shuffle the sequence:
util.shuffle(flatSequence, this.rng);
util.shuffle(flatSequence, this._randomNumberGenerator);
// reshape it into the trialSequence:
this._trialSequence = [];

View File

@ -340,17 +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
* @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
*/
export function shuffle(array, rng = undefined)
export function shuffle(array, randomNumberGenerator = undefined)
{
if (rng === undefined) {
rng = Math.random;
if (randomNumberGenerator === undefined) {
randomNumberGenerator = Math.random;
}
for (let i = array.length - 1; i > 0; i--)
{
const j = Math.floor(rng() * (i + 1));
const j = Math.floor(randomNumberGenerator() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;