Skip to content

Commit

Permalink
fix random broken by merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jaromil committed Oct 2, 2023
1 parent 7f16abe commit 632e4bc
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/lua/zencode_random.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,31 @@ When("create the array of '' random numbers", function(s)
new_codec('array', {zentype = 'a', encoding = 'integer' })
end)


When("create the array of '' random numbers modulo ''", function(s,m)
zencode_assert(not ACK.array, "Cannot overwrite existing object: ".."array")
ACK.array = { }
for i = s,1,-1 do
table.insert(ACK.array,F.new(math.floor(random_int16() % m)))
end
new_codec('array', {zentype = 'a', encoding = 'number' })
local modulo = mayhave(m)
if not modulo then
local mod = tonumber(m)
zencode_assert(mod, "Argument is not a number: "..m)
modulo = BIG.new(mod)
end
local fun
local enc
local modulo_type = type(modulo)
if modulo_type == "zenroom.big" then
fun = function(input) return BIG.random() % input end
enc = 'integer'
elseif modulo_type == "zenroom.float" then
fun = function(input) return F.new(math.floor(random_int16() % tonumber(input))) end
enc = 'float'
else
error("Modulo is not a number nor an integer: "..modulo_type)
end
_create_random_array(s, modulo, fun)
new_codec('array', {zentype = 'a', encoding = enc })
end)


local function _extract_random_elements(num, from, random_fun)
local n = tonumber(num) or tonumber(tostring(have(num)))
zencode_assert(n and n>=0, "Not a number or not a positive number: "..num)
Expand Down

0 comments on commit 632e4bc

Please sign in to comment.