Thank you for your well thought out reply!
In the world of micro-optimizations, every micromicromicrosecond is everything. Again, I know I said optimize instead of micro-optimiize in my OP; sorry about that and I have since fixed it.
- For mildly advanced programmers, they will realize this is a range function. Also, comments exist for a reason. But I do agree
:NextNumber(min, max)
is much more readable.
- Actually, I view this as a negative. There are times (none I can think of right now; sorry) that negative ranges are useful.
min + math.random() * (max-min)
will work perfectly with negative ranges. Although I am obligated to say that this could save a newbie from an hour of debugging.
- This is your best point. I honestly can’t argue it. Great job for noticing this; this is a major stumbling block that
math.random
has.
Again, I thank you for finding this problem in this beginner tutorial.
Also, I thank you for treating me like a human and not some inexperienced delinquent; I really appreciate it, thank you.
Edit: Was trying to test on some online lua compiler how much faster min + math.random() * (max-min)
is compared to Random:NextNumber(min, max)
but I realized the Random object is Roblox exclusive. I’ll try to test tomorrow or, if you want, here is the code I was going to test with:
Bad but usable comparison code
local Rand = Random.new(3463);
local timesToRepeat = 1;
local mins = {5, 2, 8, 6, 2, 8, 6, 4, 1, 4};
local maxs = {8, 3, 9, 10, 5, 9, 7, 10, 10, 23};
local tik = tick();
for z = 1, timesToRepeat do
for i = 1, #mins do
local test = mins[i] + math.random() * (maxs[i]-mins[i]);
end
end
print(tick() - tik);
local tik = tick();
for z = 1, timesToRepeat do
for i = 1, #mins do
local test = Rand:NextNumber(mins[i], maxs[i]);
end
end
print(tick() - tik);