# Error: invalid argument #2 to 'random' (interval is empty)

I’m experiencing an error:

I’m not sure why this is happening.
script:

Child creature generator

``````function MakeBabyCreature(Parent1,Parent2)
local mutationval = math.random(Parent1.Mutatibility,Parent2.Mutatibility)+math.random(-0.07,0.07)
print(Parent2)
local newCreature ={

LifeExpectancy = math.random(Parent1.LifeExpectancy,Parent2["LifeExpectancy"])+math.random(-1,1),
Fertility = math.floor(math.random(Parent1.Fertility,Parent2['Fertility'])* mutationval),
MatingDesirability = math.random(Parent1.MatingDesirability,Parent2['MatingDesirability'])* math.random(8,12)/10 * mutationval,
Mated = false,
Mutatibility = mutationval,
MateRequirements = math.floor(math.random(Parent1.MateRequirements,Parent2['MateRequirements']) * math.random(8,12)/10 * mutationval),
Cycle = 0,
Mate = nil
}
table.insert(creatures,#creatures+1,newCreature)
end

``````

Where it calls the function:

``````for i = 1, #creatures do
local creature = creatures[i]
if creature then
creature.Cycle = creature.Cycle + 1
local PotentialMate = creature.Mate
if not PotentialMate and not creature.Mated then
local Index = math.random(1,#creatures)
local maybemate = creatures[Index]
if maybemate ~= creature then
PotentialMate = maybemate
end
end

if PotentialMate and PotentialMate.MatingDesirability >= creature.MateRequirements then
for i = 1, math.min(PotentialMate.Fertility,creature.Fertility) do
if creature.Mate and type(creature.Mate) == "table" then
MakeBabyCreature(creature,creature.Mate)
end
--PotentialMate.Fertility,creature.Fertility = PotentialMate.Fertility-1,creature.Fertility-1

end
creature.Mate = PotentialMate
creature.Mated = true
PotentialMate.Mated = true
end
if creature.Cycle > creature.LifeExpectancy then
table.remove(creatures,i)
end
end
end
``````

What is line 23? It is hard to tell.

Assuming line 23 is `math.random(1,#creatures)`, it means `#creatures` is 0. The error means that you’re trying to generate a random number but the second value is lesser than the first.

``````-- in the child generator function, line 23
Fertility = math.floor(math.random(Parent1.Fertility,Parent2['Fertility'])* mutationval),
``````

What is Parent2[‘Fertility’], is it a number?

It means Parent2.Fertility < Parent1.Fertility, an easy solution would be to:
`math.random(math.min(Parent1.Fertility, Parent2.Fertility), math.max(Parent1.Fertility, Parent2.Fertility))`

That might be the case, which means I have to add a bunch of `math.min ` and `math.max`
I’ll check it out later