replace lua Random implementation with usertype
This commit is contained in:
+30
-1
@@ -669,4 +669,33 @@ end
|
||||
bit.compile = require "core:bitwise/compiler"
|
||||
bit.execute = require "core:bitwise/executor"
|
||||
|
||||
random.Random = require "core:internal/random_generator"
|
||||
function __vc_create_random_methods(random_methods)
|
||||
local index = 1
|
||||
local buffer = nil
|
||||
local buffer_size = 64
|
||||
|
||||
function random_methods:bytes(n)
|
||||
local bytes = Bytearray(n)
|
||||
for i=1,n do
|
||||
bytes[i] = self:random(255)
|
||||
end
|
||||
return bytes
|
||||
end
|
||||
|
||||
function random_methods:random(a, b)
|
||||
if not buffer or index > #buffer then
|
||||
buffer = self:generate(buffer_size)
|
||||
index = 1
|
||||
end
|
||||
local value = buffer[index]
|
||||
if b then
|
||||
value = math.floor(value * (b - a + 1) + a)
|
||||
elseif a then
|
||||
value = math.floor(value * a + 1)
|
||||
end
|
||||
|
||||
index = index + 4
|
||||
return value
|
||||
end
|
||||
return random_methods
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user