Created
July 21, 2015 03:02
-
-
Save rangercyh/2db7b6e6c21fd58c78c8 to your computer and use it in GitHub Desktop.
mt random
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
var MT = []; | |
var index = 0; | |
function initialize_generator(seed) { | |
MT[0] = seed; | |
for (var i = 1; i < 624; i++) { | |
MT[i] = 0xffffffff & (0x6c078965 * (MT[i - 1] ^ (MT[i - 1] >> 30)) + i); | |
} | |
} | |
function generate_numbers() { | |
for (var i = 0; i < 624; i++) { | |
var y = (MT[i] & 0x80000000) + (MT[(i + 1) % 624] & 0x7fffffff); | |
MT[i] = MT[(i + 397) % 624] ^ (y >> 1); | |
if (y % 2 != 0) { | |
MT[i] ^= 0x9908b0df; | |
} | |
} | |
} | |
function extract_number() { | |
if (index == 0) { | |
generate_numbers(); | |
} | |
var y = MT[index]; | |
y ^= (y >> 11); | |
y ^= ((y << 7) & 0x9d2c5680); | |
y ^= ((y << 15) & 0xefc60000); | |
y ^= (y >> 18); | |
index = (index + 1) % 624; | |
return y; | |
} | |
function mt_rand(min, max) { | |
return extract_number() % (max - min + 1) + min; | |
} | |
initialize_generator(new Date().getTime()); | |
for (var i = 0; i < 10; ++i) { | |
mt_rand(1, 9999); | |
} | |
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
local MT = {} | |
local index = 0 | |
function initialize_generator(seed) | |
MT[0] = seed | |
for i = 1, 623 do | |
MT[i] = 0xffffffff & (0x6c078965 * (MT[i - 1] ~ (MT[i - 1] >> 30)) + i) | |
end | |
end | |
function generate_numbers() | |
for i = 1, 623 do | |
local y = (MT[i] & 0x80000000) + (MT[(i + 1) % 624] & 0x7fffffff) | |
MT[i] = MT[(i + 397) % 624] ~ (y >> 1) | |
if (y % 2) ~= 0 then | |
MT[i] = MT[i] ~ 0x9908b0df | |
end | |
end | |
end | |
function extract_number() | |
if index == 0 then | |
generate_numbers() | |
end | |
local y = MT[index] | |
y = y ~ (y >> 11) | |
y = y ~ ((y << 7) & 0x9d2c5680) | |
y = y ~ ((y << 15) & 0xefc60000) | |
y = y ~ (y >> 18) | |
index = (index + 1) % 624 | |
return y | |
end | |
function mt_rand(min, max) | |
return extract_number() % (max - min + 1) + min | |
end | |
initialize_generator(os.time()) | |
for i = 1, 10 do | |
print(mt_rand(1, 9999)) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment