bad argument #1 to 'random' (interval is empty)?!?!?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

bad argument #1 to 'random' (interval is empty)?!?!?

Post 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)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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.
Help us help you: attach a .love.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post by Robin »

I'm sorry, it's hard to help you if we can't tell what this code is supposed to do.
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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)).." ")
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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).." ")
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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.
When I write def I mean function.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: bad argument #1 to 'random' (interval is empty)?!?!?

Post 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:

Code: Select all

value=math.random(0,20)-10
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests