| Line 1: |
Line 1: |
| | + | -- [[Category:Net modules]] |
| | + | |
| | local p = {} | | local p = {} |
| | + | |
| | + | local bit32 = require( 'bit32' ) |
| | + | |
| | -- Module for random library functions for lua because lua sucks. Written by | | -- Module for random library functions for lua because lua sucks. Written by |
| | -- Remi_Scarlet | | -- Remi_Scarlet |
| | -- I fucking hate lua. | | -- I fucking hate lua. |
| | | | |
| | + | -- Takes a table. Returns a bool of whether it's empty or not. |
| | + | function table.empty (self) |
| | + | for _, _ in pairs(self) do |
| | + | return false |
| | + | end |
| | + | return true |
| | + | end |
| | + | |
| | + | function table.getKeys(self) |
| | + | local keys = {} |
| | + | for k,_ in pairs(self) do |
| | + | table.insert(keys,k) |
| | + | end |
| | + | return keys |
| | + | end |
| | + | |
| | + | function table.getVals(self) |
| | + | local vals = {} |
| | + | for _,v in pairs(self) do |
| | + | table.insert(vals,v) |
| | + | end |
| | + | return vals |
| | + | end |
| | | | |
| | -- takes a string and returns string with first letter capitalized | | -- takes a string and returns string with first letter capitalized |
| Line 10: |
Line 38: |
| | end | | end |
| | | | |
| − | function p.timeHash() | + | function p.hashString(str) |
| | + | local hash = 0 |
| | + | if str == nil or str == "" then |
| | + | return 0 |
| | + | end |
| | + | if type(str) == "string" and str ~= "" then |
| | + | for i = 1, #str do |
| | + | local letter = str:sub(i,i) |
| | + | hash = bit32.lshift(hash,3) |
| | + | hash = hash+str.byte(letter) % (2^31-1) |
| | + | end |
| | + | end |
| | + | return hash |
| | + | end |
| | + | |
| | + | |
| | + | -- Hashes a seed if given, if not just returns a hash based on current time. |
| | + | function p.timeHash(seed) |
| | + | -- lmfao this function wasn't a hash at all because it would never |
| | + | -- return the same thing twice. |
| | + | math.randomseed(p.hashString("Amatsukaze-chan is best")) |
| | + | |
| | + | local randomizer = 0 |
| | + | |
| | + | if seed == nil then |
| | + | randomizer = math.random(0,9999999) |
| | + | elseif type(seed) == "table" then |
| | + | if table.empty(seed) then |
| | + | randomizer = math.random(0,9999999) |
| | + | else |
| | + | for k,v in pairs(seed) do |
| | + | if type(k) == "number" then |
| | + | randomizer = randomizer + k |
| | + | elseif type(k) == "string" then |
| | + | randomizer = randomizer + p.hashString(k) |
| | + | end |
| | + | if type(v) == "string" then |
| | + | randomizer = randomizer + p.hashString(v) |
| | + | elseif type(v) == "number" then |
| | + | randomizer = randomizer + v |
| | + | elseif type(v) == "table" then |
| | + | local tableString = p.dictConcat(v) |
| | + | randomizer = randomizer + p.hashString(v) |
| | + | end |
| | + | end |
| | + | end |
| | + | elseif type(seed) == "string" then |
| | + | randomizer = p.hashString(seed) |
| | + | elseif type(seed) == "number" then |
| | + | randomizer = seed |
| | + | end |
| | local time = os.time() | | local time = os.time() |
| | local rand = math.random() | | local rand = math.random() |
| − | return math.floor(time*2653145821*rand) % 2^32 | + | local rtnHash = (math.floor(time*5501*rand)-math.floor(randomizer*time) + math.floor(math.random()*5501*randomizer)) % 2^32 |
| | + | return rtnHash |
| | end | | end |
| | | | |