我做了一个随机数生成器,它足够好还是我可以让它更随机?
I made a random number generator, is it good enough or can I make it more randomized?
local z = 50 -- you can change this to the maximum value
local function ARN() -- function starts
local x = math.random(0,z) -- main number
local y = math.random() -- decimal number
local v = (x-(y)) -- I remove y's value from x
print(v) -- I then print it
return v, x, y -- return the values
end
ARN() -- calling the function
我基本上也是用十进制数随机生成的。我不知道是否有什么可以让它更随机化。
另外,我是编程新手,所以也许一些提示也会有所帮助!
math.random()
已经 returns 一个 floating-point 介于 0(含)和 1(不含)之间的随机数。您显然想要一个从 0 到 z
的 floating-point 随机数。简单地通过将随机数与 z
相乘来“缩放”随机数应该就足够了,并且只需要一次调用 math.random
:
local function ARN() return math.random() * z end
这可能会略微降低浮点部分的随机性,但对于小的 z
应该不会很明显。
您的实施目前是从 0(不含)到 z
(含)。在实践中,这可能很少重要,但这些是预期的界限吗?我提出的缩放将与 math.random
一致,其中 0 是排他性的,z
是包容性的。
至于“随机性”:math.random
使用 xoshiro256** 而您几乎只使用 math.random
,因此您应该具有大致相同的随机性。不过,它只是一个“伪”而不是“安全”随机数。
也可以考虑使用 math.randomseed(os.time() + os.clock())
或类似的方法进行随机播种;请注意,种子是全局的,是否要播种取决于您的应用程序(出于测试目的,您可能需要固定的种子 f.E)。
local z = 50 -- you can change this to the maximum value
local function ARN() -- function starts
local x = math.random(0,z) -- main number
local y = math.random() -- decimal number
local v = (x-(y)) -- I remove y's value from x
print(v) -- I then print it
return v, x, y -- return the values
end
ARN() -- calling the function
我基本上也是用十进制数随机生成的。我不知道是否有什么可以让它更随机化。 另外,我是编程新手,所以也许一些提示也会有所帮助!
math.random()
已经 returns 一个 floating-point 介于 0(含)和 1(不含)之间的随机数。您显然想要一个从 0 到 z
的 floating-point 随机数。简单地通过将随机数与 z
相乘来“缩放”随机数应该就足够了,并且只需要一次调用 math.random
:
local function ARN() return math.random() * z end
这可能会略微降低浮点部分的随机性,但对于小的 z
应该不会很明显。
您的实施目前是从 0(不含)到 z
(含)。在实践中,这可能很少重要,但这些是预期的界限吗?我提出的缩放将与 math.random
一致,其中 0 是排他性的,z
是包容性的。
至于“随机性”:math.random
使用 xoshiro256** 而您几乎只使用 math.random
,因此您应该具有大致相同的随机性。不过,它只是一个“伪”而不是“安全”随机数。
也可以考虑使用 math.randomseed(os.time() + os.clock())
或类似的方法进行随机播种;请注意,种子是全局的,是否要播种取决于您的应用程序(出于测试目的,您可能需要固定的种子 f.E)。