So I was looking for a math.random
function that doesn’t generate the same number after being repeated a couple of times and found this. I modified it a bit and it still generates the same number. Can anyone tell me whats wrong in this code?
function coolFunctions.generateMines(count, min, max)
local randomNumbers = {}
for index = 1, count do
local randomNumber
local nCount = 0
repeat
randomNumber = math.random(min, max)
nCount += 1
until not (table.find(randomNumbers, randomNumber) and nCount == count)
table.insert(randomNumbers, randomNumber)
end
return randomNumbers
end
I am a bit confused about what you’re trying to achieve?
Just use math.random
. It does not generate the same number, it generates random numbers.
If you want more variability in the numbers you get, you can multiply the random range by a number, and divide the result by that number:
print(math.random(1,5)) -- 1,2,3,4,5
print(math.random(10,50)/10) -- 1, 1.1, 1.2, 1.3, ..., 4.9, 5
3 Likes
I actually think OP wants so, that generated numbers does not get generated again.
This should do it:
local function GenerateRandomNumbers(AmountToGenerate,Min,Max)
local RandomNumbers = {}
for Number = 1, AmountToGenerate do
local RandomNumber
repeat
RandomNumber = math.random(Min,Max)
until not table.find(RandomNumbers, RandomNumber)
table.insert(RandomNumbers, RandomNumber)
end
return RandomNumbers
end)
Be aware that your magnitude between Min & Max, should always be equal to or greater than AmountToGenerate.
1 Like
That’s not how RNG works.
Summary
This text will be hidden
No, that is not how plain RNG works, but it will still be a random number. If you roll twice with a dice, but exclude having two of the same numbers, then both rolls will still be random - because if you roll the same number twice, then you just reroll until you get another random number. The pool of numbers are just lesser.
I think you’ve misunderstood my statement. I think OP wants to roll random numbers more than once, and make sure to always pick new numbers, that haven’t been picked before. math.random does indeed roll a random number, but if you use math.random twice with the same criterias, then there is a chance for it to roll the same number.
I don’t think that’s a bad thing since it’s still random.
Sorry Jax, but we gotta focus on what OP wants. So what you and I thinks, does not really matter. As long as OP got his/her solution. If OP needs a table filled with random numbers, but also wants to make sure that two numbers aren’t the same, then we gotta try to deliver that solution.
1 Like
If only the OP responded, then we could progress in helping him. His function to generate random (but unique) numbers is called generateMines
. So, maybe he’s trying to make some sort of minefield where no mines are touching each other.
1 Like
Maybe this function will work?
local function genUniqueNumbers(amount, min, max)
local list = {}
while #list < amount do
local num = math.random(min, max)
if not table.find(list, num) then
table.insert(list, num)
end
-- Prevent crashes if you can't possibly generate new unique numbers
if #list == abs(max - min) + 1 then break end
end
return list
end