Last active
December 17, 2015 23:29
-
-
Save TomK32/5689884 to your computer and use it in GitHub Desktop.
Park-Miller-Carta PNRG
This file contains hidden or 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
| --[[ | |
| Copyright (c) 2009 Michael Baczynski, http://www.polygonal.de | |
| Permission is hereby granted, free of charge, to any person obtaining | |
| a copy of this software and associated documentation files (the | |
| "Software"), to deal in the Software without restriction, including | |
| without limitation the rights to use, copy, modify, merge, publish, | |
| distribute, sublicense, and/or sell copies of the Software, and to | |
| permit persons to whom the Software is furnished to do so, subject to | |
| the following conditions: | |
| The above copyright notice and this permission notice shall be | |
| included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| --]] | |
| -- Implementation of the Park Miller (1988) "minimal standard" linear | |
| -- congruential pseudo-random number generator. | |
| -- @author Michael Baczynski, www.polygonal.de | |
| -- @author Thomas R. Koll, www.ananasblau.com | |
| -- MIT License | |
| local PM_PRNG | |
| do | |
| local _parent_0 = nil | |
| local _base_0 = { | |
| prime = math.pow(2, 31) - 1, | |
| nextInt = function(self) | |
| return self:generate() | |
| end, | |
| nextDouble = function(self) | |
| return self:generate() / self.prime | |
| end, | |
| nextIntRange = function(self, min, max) | |
| min = min - 0.4999 | |
| max = max + 0.4999 | |
| return math.floor(0.5 + min + ((max - min) * self:nextDouble())) | |
| end, | |
| nextDoubleRange = function(self, min, max) | |
| return min + ((max - min) * self:nextDouble()) | |
| end, | |
| generate = function(self) | |
| self.seed = (self.seed * 16807) % self.prime | |
| return self.seed | |
| end | |
| } | |
| _base_0.__index = _base_0 | |
| if _parent_0 then | |
| setmetatable(_base_0, _parent_0.__base) | |
| end | |
| local _class_0 = setmetatable({ | |
| __init = function(self, seed) | |
| self.seed = seed or 1 | |
| end, | |
| __base = _base_0, | |
| __name = "PM_PRNG", | |
| __parent = _parent_0 | |
| }, { | |
| __index = function(cls, name) | |
| local val = rawget(_base_0, name) | |
| if val == nil and _parent_0 then | |
| return _parent_0[name] | |
| else | |
| return val | |
| end | |
| end, | |
| __call = function(cls, ...) | |
| local _self_0 = setmetatable({}, _base_0) | |
| cls.__init(_self_0, ...) | |
| return _self_0 | |
| end | |
| }) | |
| _base_0.__class = _class_0 | |
| if _parent_0 and _parent_0.__inherited then | |
| _parent_0.__inherited(_parent_0, _class_0) | |
| end | |
| PM_PRNG = _class_0 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment