mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
replace lua Random implementation with usertype
This commit is contained in:
parent
3a40162f1e
commit
088bf63f3f
7 changed files with 115 additions and 38 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue