Page 1 of 2
bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 10:47 am
by TheBreadCat
im making a 1d terrain genarator.
but there is a problem:
Code: Select all
hm = {}--height map
hp = {}--height points
lp = {}--low points
mf = 2--mutate factor
function init()
math.randomseed( os.time() )
for i = 0 , 9 do
hm[i] = {}
end
for i = 0 , 9 do
hm[i] = math.random(0,9)
end
end
init()
for i = 0 , 9 do
pon = math.random(0,1)
if i == 0 then
io.write(math.random(math.floor((0 - hm[i+1])/2)))
elseif i == 9 then
io.write(math.random(math.floor((hm[i-1] - 0)/2)))
else
io.write(math.random(math.floor((hm[i-1] - hm[i+1])/2)).." ")
end
io.write(hm[i].." ")
end
Code: Select all
lua: c.lua:20: bad argument #1 to 'random' (interval is empty)
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 11:38 am
by Robin
The problem is probably in one of the lines:
Code: Select all
io.write(math.random(math.floor((0 - hm[i+1])/2)))
io.write(math.random(math.floor((hm[i-1] - 0)/2)))
io.write(math.random(math.floor((hm[i-1] - hm[i+1])/2)).." ")
When calling math.random with a single argument, make sure that argument is > 0. Perhaps doing something like
Code: Select all
io.write(math.random(math.floor((0 - hm[i+1])/2)+1))
io.write(math.random(math.floor((hm[i-1] - 0)/2)+1))
io.write(math.random(math.floor((hm[i-1] - hm[i+1])/2+1)).." ")
Although I don't really understand the purpose of this code.
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 11:53 am
by Taehl
Apparently the math in your call to math.random on line 20 is resulting in a number that's less than one. Make sure it's greater than or equal to 1.
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 12:54 pm
by TheBreadCat
Taehl wrote:Apparently the math in your call to math.random on line 20 is resulting in a number that's less than one. Make sure it's greater than or equal to 1.
but i need a negative value.
so do i have to make a if statement to check if the value is negative , and if so then set it to the minimum of the random function?
like this:
Code: Select all
for i = 0 , 9 do
pon = math.random(0,1)
if i == 0 then
if math.floor((0 - hm[i+1])/2) < 0 then
io.write(math.random(math.floor((0 - hm[i+1])/2)),0)
else
io.write(math.random(math.floor((0 - hm[i+1])/2)))
end
elseif i == 9 then
if math.floor((0 - hm[i+1])/2) < 0 then
io.write(math.random(math.floor((0 - hm[i-1])/2)),0)
else
io.write(math.random(math.floor((0 - hm[i-1])/2)))
end
else
if math.floor((0 - hm[i+1])/2) < 0 then
io.write(math.random(math.floor((hm[i-1] - hm[i+1])/2),0).." ")
else
io.write(math.random(math.floor((0 - hm[i+1])/2)))
end
end
io.write(hm[i].." ")
end
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 1:52 pm
by Robin
I'm sorry, it's hard to help you if we can't tell what this code is supposed to do.
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 7:01 pm
by BlackBulletIV
A solution that will work is to use the opposite positive number for math.random and then negate the result to get it back to negative. If you need to get a negative number to positive you can use the negation operator, and the same for getting it back to negative. For example:
Code: Select all
io.write(-math.random(-math.floor((hm[i-1] - hm[i+1])/2)).." ")
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 9:49 pm
by Robin
BlackBulletIV wrote:A solution that will work is to use the opposite positive number for math.random and then negate the result to get it back to negative. If you need to get a negative number to positive you can use the negation operator, and the same for getting it back to negative. For example:
Code: Select all
io.write(-math.random(-math.floor((hm[i-1] - hm[i+1])/2)).." ")
I'm not sure if what you are doing is what the OP wants, but if so: I think this also works:
Code: Select all
io.write(math.random(math.floor((hm[i-1] - hm[i+1])/2), 0).." ")
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 10:03 pm
by kikito
TheBreadCat wrote:
but i need a negative value.
This might sound a bit obvious, but there it goes - if you multiply any positive value with -1 you get a negative value.
Code: Select all
my_negative_value = my_positive_value * -1
As others are saying, knowing the context of your problem will probably help us help you better.
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Sun Feb 06, 2011 10:24 pm
by BlackBulletIV
Robin wrote:BlackBulletIV wrote:A solution that will work is to use the opposite positive number for math.random and then negate the result to get it back to negative. If you need to get a negative number to positive you can use the negation operator, and the same for getting it back to negative. For example:
Code: Select all
io.write(-math.random(-math.floor((hm[i-1] - hm[i+1])/2)).." ")
I'm not sure if what you are doing is what the OP wants, but if so: I think this also works:
Code: Select all
io.write(math.random(math.floor((hm[i-1] - hm[i+1])/2), 0).." ")
Negating a number twice will you the same number. But if that works that's cool.
kikito wrote:TheBreadCat wrote:
but i need a negative value.
This might sound a bit obvious, but there it goes - if you multiply any positive value with -1 you get a negative value.
Code: Select all
my_negative_value = my_positive_value * -1
As others are saying, knowing the context of your problem will probably help us help you better.
Or you could use the negation operator (-):
Code: Select all
my_negative_value = -my_positive_value
Re: bad argument #1 to 'random' (interval is empty)?!?!?
Posted: Tue Feb 08, 2011 3:36 pm
by miko
TheBreadCat wrote:Taehl wrote:Apparently the math in your call to math.random on line 20 is resulting in a number that's less than one. Make sure it's greater than or equal to 1.
but i need a negative value.
But math.random() needs a positive value.
If you want to get a random number between -10 and 10 do something like this: