Is there a way to generate numbers non repeating in a random order? E.g. if I wanted to generated 1-5 in a random order:
1,3,2,5,4
Couldn’t you do this?
For a Simple Description:
math.random
generates a random number between the giving arguments:
math.random(Lowest Number, Max Number)
so you could do: math.random(0,10)
and it will give anumber between that scope
local RandomNumber = math.random(0,9) -- returns a random number between 0 and 9
print(RandomNumber)
Also this:
print(math.random(0,9),math.random(0,9),math.random(0,9),math.random(0,9)) -- returns a random number between 0 and 9 but with 4 Digits
You can also do this:
function R()
return math.random(0,9) -- returns a random number between 0 and 9
end
print(R(),R(),R(),R())
Or this (same thing as the Second but more Simplified):
print(math.random(1111, 9999)
The math
function According to Documentation:
Yeah, I understand what math.random does but it can repeat numbers. E.g. 4, 5, 9, 4
Yes, thats the point, it gives a random number between the Number Range given, due to this, it can lead to copies of numbers
Thats not what im wondering though, I was wondering if theres a way to have it not repeat.
Ok so this is a very ugly way of doing it but it might work:
local TN = {[1]=nil;[2]=nil;[3]=nil;[4]=nil}
local function GenerateNumber()
local Key = math.random(0,9)
if Key ~= TN[1] and Key ~= TN[2] and Key ~= TN[3] and Key ~= TN[4] then
return Key
else
repeat wait() Key = math.random(0,9) until Key ~= TN[1] and Key ~= TN[2] and Key ~= TN[3] and Key ~= TN[4]
end
return Key
end
while wait() do -- Loops to print a bunch of numbers that shouldnt equal any of the Tables Numbers
TN[1] = GenerateNumber()
TN[2] = GenerateNumber()
TN[3] = GenerateNumber()
TN[4] = GenerateNumber()
print(table.unpack(TN))
end
You could create an array table consisting of non-repeating numbers in any permutation and then use shuffling algorithms like Fisher-Yates to shuffle the table.
Thanks for the help, ill mess around with it and see how it works out and lyk
You can do something like this.
local length = 5
local list = { }
function generate()
function add(n:number)
if not table.find(list) then
table.insert(list,n)
else
add(math.random(1,length))
end
end
for i = 1, length do
add(math.random(1,length)
end
end
Does anybody else have any ideas?
That specific task is super easy.
local result = {}
-- for numbers 1 through 5
for n = 1, 5 do
-- insert the number at a random position
-- math.max ensures it still works when #result is 0
table.insert(result, math.random(math.max(1, #result)), n)
end
print(result)
This isn’t very efficient with extremely large tables. You’re fine unless you’re doing this on tables that have ten thousand or so entries and you’re trying to do it as fast as possible. When you get to that point, benchmark it and see if it will cause issues. That said, I think it’s still the fastest one that has been posted so far.
print(math.random(1,5))
then look in your output tab to see the number
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.