randomSeed in `math.config` does not trigger an update when the same value is used
- Dominant language
- JavaScript
- Stars
- 15.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
The randomSeed option of the `math.config` call does not seem to trigger an update when the same value is used.
```
// Checked with the browser
math.version
> "7.5.1"
math.config({randomSeed: 100}); math.random(1);
> 0.6781078499227314
math.config({randomSeed: 100}); math.random(1);
> 0.35155301939509676
// Reset
math.config({randomSeed: null}); math.random(1);
> 0.7179467920588589
math.config({randomSeed: 100}); math.random(1);
> 0.6781078499227314
```
```
// Checked with Node.js
> const {create, all, version} = require("mathjs")
undefined
> version
'7.5.1'
> const math = create(all, {})
undefined
> math.config({randomSeed:100}); math.random();
0.6781078499227314
> math.config({randomSeed:100}); math.random();
0.35155301939509676
// Reset
> math.config({randomSeed:null}); math.random();
0.749174817891811
> math.config({randomSeed:100}); math.random();
0.6781078499227314
```
The issue has been addressed in issue [799](https://github.com/josdejong/mathjs/pull/799#issuecomment-282187641), but the problem seems to come back at #1319 with the addition of `curr.randomSeed !== prev.randomSeed` in [random.js, randomInt.js and pickRandom.js](https://github.com/josdejong/mathjs/search?q=curr.randomSeed+%21%3D%3D+prev.randomSeed).
The check is needed because "randomSeed" in math.config remains a constant, so when the random number generator (RNG) is created on the fly, it has to avoid starting the RNG from scratch, or the generated numbers will also be constants.
~A workaround is perhaps to use some kind of system time as the default seed when the RNG is initiated, but the current choice may have been made to ensure cross-platform compatibility. So I am not sure if this is a feature or a defect. I wonder if #1955 would fix it automatically.~
Edit: On closer inspection, it seems both seed-random and seedrandom would auto-seed when no argument is provided, so the above is relevant only when "randomSeed" is set to something not equal to null. I apologise if I did not explain the issue clearly. In short, I think a clarification is needed on what one should expect when randomSeed is set in math.config. Does it mean
- the seed is set persistently the value provided, as the value in math.config is indeed persistent? or
- the seed is set one time at the time it is provided (which I imagine is what users expect)?
I am confused because the current code seems to do a bit of both. It fails the first one, because when a seed is set, the first and second runs of `random` gives different results, and it fails the second one, because if the same seed is provided twice in a row, it does not give the same result.
While on the topic, may I also ask if there is a way to set the seed if I import only the random function from mathjs? I expect negative but would be happy to be corrected. Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.