Update Random with faster implementation + cleanup #1284
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This replaces the current Mersenne Twister with a new pseudorandom number generator known as Small Fast Counter 32. SFC32 seemed to be the best all around PRNG that I could find that worked well across the targets I can test on (HTML5 Electron and Firefox, Windows...yeah still limited here, sorry).
SFC32 was always faster or generated more numbers in a shorter time period than the current implementation in a few various test cases that I tried out which are all obviously contrived and more about trying to identify a noticeable difference rather than trying to extract an exact value. Those cases were the following:
For case 1 and 2, this was done over a period of 10 seconds 60 times per second. This was using a timetask, however these tests weren't stressful enough to trigger a slowdown which would mess with the final timings (I made sure to check for that). For case 3, I basically just ended up getting bottlenecked somewhere across all targets for both implementations. Dunno why, don't really care because SFC32 still wins.
For every case, the values are rough and eyeballed, but still show a trend. They were each also run once before hand before measurements were taken.
Test Case 1 (Vectors): Cumulative time spent
Test Case 2 (Int Array): Cumulative time spent
Test Case 3 (Shit ton of ints over 10 seconds): Total count
SFC32 also comes with some bonuses like being more cryptographically secure than the Mersenne Twister according to some smart people who aren't me, and it is a public domain algorithm. You can verify that here: http://pracrand.sourceforge.net/
The implementation I used was derived from here, and slightly modified: https://github.com/bryc/code/blob/master/jshash/PRNGs.md#sfc32. Removing a few of the bitwise ORs resulted in no change to the output but slightly increased speed. The rest of them needed to remain, as removing them resulted in a slight speed decrease for the HTML5 targets.
Hopefully, this performance holds up across all targets that I am unable to test.
Thanks for reading my blog, have a nice day.