Currently I use a system to generate items for my game, there will likely be 10 million items generated per second at the system’s max. I use a math.random() which returns a float then ofc a weight system to an determine the object. If this takes up a large amount of processing power I would likely make a system that assigned each item an amount to be generated.
math.random is one of the fastest operators but if your really worried just batch the calls
It’s about 13x slower than doing addition, which is pretty good. You should be fine
Time normally using math.random: 2.9021108150482178
Time normally using math.random assigned to a var: 2.89693021774292
Time normally using Random: 3.327341079711914
Time to add (benchmark time): 0.23066139221191406
Test code
local n = 100000000
do
local initial = tick()
for i = 1, n do
local _ = math.random()
end
print("Time normally using math.random:", tick() - initial)
end
do
local initial = tick()
local r = math.random
for i = 1, n do
local _ = r()
end
print("Time normally using math.random assigned to a var:", tick() - initial)
end
do
local initial = tick()
local r = Random.new()
for i = 1, n do
local _ = r:NextNumber()
end
print("Time normally using Random:", tick() - initial)
end
do
local initial = tick()
for i = 1, n do
local _ = 500+350
end
print("Time to add (benchmark time):", tick() - initial)
end