From 20b8961640b6c22d4149f7268d403cb4609a03fb Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sun, 7 Jun 2020 18:34:21 +0200 Subject: New file Structure --- random.lua | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 random.lua (limited to 'random.lua') diff --git a/random.lua b/random.lua deleted file mode 100644 index 4fbbed3..0000000 --- a/random.lua +++ /dev/null @@ -1,40 +0,0 @@ -skycraft.random = { - choices = {}, - probabilities = {}, - csum = {}, - sum = 0 -} - -skycraft.random.__index = skycraft.random - -function skycraft.random:new(o) - o = o or {} - setmetatable(o, self) - o.choices = {} - o.probabilities = {} - o.csum = {} - o.sum = 0 - return o -end - -function skycraft.random:calc_csum() - self.sum = 0 - for i, choice in ipairs(self.choices) do - self.sum = self.sum + self.probabilities[choice] - self.csum[choice] = self.sum - end -end - -function skycraft.random:choose() - local r = math.random() + math.random(0, self.sum - 1) - for i, choice in pairs(self.choices) do - if r < self.csum[choice] then - return choice - end - end -end - -function skycraft.random:add_choice(choice, probability) - table.insert(self.choices, choice) - self.probabilities[choice] = probability -end -- cgit v1.2.3