mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-10 19:20:55 +00:00
use string as seed
This commit is contained in:
parent
f2f9a00ae0
commit
2cea5640f7
@ -477,7 +477,7 @@ jsPsych.randomization.setSeed(seed)
|
|||||||
|
|
||||||
| Parameter | Type | Description |
|
| Parameter | Type | Description |
|
||||||
| --------- | ----- | ------------------------------ |
|
| --------- | ----- | ------------------------------ |
|
||||||
| seed | string or number | A seed for the random number generator |
|
| seed | string | A seed for the random number generator |
|
||||||
|
|
||||||
### Return value
|
### Return value
|
||||||
|
|
||||||
@ -495,12 +495,19 @@ The seed value will be returned from the function call, allowing you to save it
|
|||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
|
||||||
#### Shuffle an array
|
#### Use a random 32-bit seed and save to data
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var myArray = [1,2,3,4,5];
|
const seed = jsPsych.setSeed();
|
||||||
var shuffledArray = jsPsych.randomization.shuffle(myArray);
|
jsPsych.data.addProperties({
|
||||||
// output: shuffledArray = [3,2,4,1,5]
|
rng_seed: seed
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Use your own seed
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
jsPsych.setSeed("jspsych");
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
@ -7,10 +7,10 @@ import seedrandom from "seedrandom";
|
|||||||
* @param seed An optional seed. If none is given, a random seed will be generated.
|
* @param seed An optional seed. If none is given, a random seed will be generated.
|
||||||
* @returns The seed value.
|
* @returns The seed value.
|
||||||
*/
|
*/
|
||||||
export function setSeed(seed?) {
|
export function setSeed(seed?: string) {
|
||||||
if (!seed) {
|
if (!seed) {
|
||||||
const prng = seedrandom();
|
const prng = seedrandom();
|
||||||
seed = prng.int32();
|
seed = prng.int32().toString();
|
||||||
}
|
}
|
||||||
seedrandom(seed, { global: true });
|
seedrandom(seed, { global: true });
|
||||||
return seed;
|
return seed;
|
||||||
|
Loading…
Reference in New Issue
Block a user