-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move PRNG outside framebox and improve usage
- Loading branch information
Showing
6 changed files
with
52 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
-- | ||
-- Pseudo-Random Number Generator (PRNG) | ||
-- License: MIT | ||
-- 2022, 2023 Didier Willis | ||
-- | ||
-- Why would a text processing software such as SILE need a PRNG, | ||
-- where one would expect the reproduceability of the output? | ||
-- | ||
-- Well, there are algorithms were a bit of randomness is expected | ||
-- e.g. the rough "hand-drawn-like" drawing style, where one would | ||
-- expect all rough graphics to look different. | ||
-- But using math.random() there would yield always different results... | ||
-- There are algorithms were a bit of randomness is expected, but | ||
-- where one would expect a reproducible output. | ||
-- Using math.random() there would yield always different results... | ||
-- and using math.randomseed() is also problematic: it's global and could be | ||
-- affected elsewhere, etc. | ||
-- So one may need instead a "fake" PRNG, that spits out a seemingly uniform | ||
-- distribution of "random" numbers. | ||
|
||
-- ([email protected]) The algorithm below was just found on the | ||
-- Internet, where it was stated to be common in Monte Carlo randomizations. | ||
-- | ||
-- The algorithm below was just found on the Internet, where it was stated to | ||
-- be "common in Monte Carlo randomizations." | ||
-- | ||
-- I am not so lazy not to check, and traced it back to Sergei M. Prigarin, | ||
-- _Spectral Models of Random Fields in Monte Carlo Methods_, 2001. | ||
|
@@ -25,8 +22,8 @@ | |
-- This derivation, if I read correctly, has a 2^40 module and 5^17 mutiplier | ||
-- (cycle length 2^38). | ||
-- For information; the seeds are (X1, X2), here set to (0, 1). The algorithm | ||
-- could be seeded with other values. It's not clear to me which variant was | ||
-- used (I didn't check the whole book...), but it seems the constraints are | ||
-- can be seeded with other values. | ||
-- I didn't check the whole book...), but it seems the constraints are | ||
-- 0 < X1, X2 <= 2^20 and X2 being odd. | ||
|
||
local A1, A2 = 727595, 798405 -- 5^17=D20*A1+A2 | ||
|
@@ -35,6 +32,11 @@ local D20, D40 = 1048576, 1099511627776 -- 2^20, 2^40 | |
local PRNG = pl.class({ | ||
X1 = 0, | ||
X2 = 1, | ||
_init = function (self, seed) | ||
if seed then -- Just seeding X1 | ||
self.X1 = math.abs(seed) % D20 | ||
end | ||
end, | ||
random = function (self) | ||
local U = self.X2 * A2 | ||
local V = (self.X1 * A2 + self.X2 * A1) % D20 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters