Jump to content




[SOLVED] Problem with math.random()


  • You cannot reply to this topic
5 replies to this topic

#1 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 25 June 2016 - 06:21 PM

So I'm trying to create and animate fire using random positions but I am getting some error
test.lua:5: bad argument #2: interval is empty
I've checked the code enough times and i dont get the problem...

function fire(x,y,height,spreadRad,frames,nLines,colors_back)
for a = 1,frames do
  for e = 1,nLines do
   g = math.random(x-1,x-spreadRad)
   j = math.random(y-1,y-height)
   paintutils.drawLine(x,y,g,j,colors.red)
  end
  sleep(0.003)
  term.setBackgroundColor(colors_back)
  term.clear()
end
end

fire(35,19,5,5,13,6,colors.black)

hope I'll get an answer that I'll understand because.... dafruq ;)

Edited by ReBraLaCC, 26 June 2016 - 04:09 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 25 June 2016 - 07:08 PM

That error is caused by the second argument being less than the first argument. For example,
math.random( 1, 0 )


#3 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 25 June 2016 - 07:46 PM

View PostKingofGamesYami, on 25 June 2016 - 07:08 PM, said:

That error is caused by the second argument being less than the first argument.

thought of that, the fire code at the end is the code I am really using.... Ughhh errors that make no sense ;-;

#4 NanoBob

  • Members
  • 102 posts

Posted 25 June 2016 - 08:21 PM

View PostReBraLaCC, on 25 June 2016 - 07:46 PM, said:

View PostKingofGamesYami, on 25 June 2016 - 07:08 PM, said:

That error is caused by the second argument being less than the first argument.

thought of that, the fire code at the end is the code I am really using.... Ughhh errors that make no sense ;-;
It does in fact make sense. You call
math.random(x-1,x-spreadRad)

The first parameter, x is 35. And spreadRad (third parameter) is 5.
fire(35,19,5,5,13,6,colors.black)

So your math random is basicly
math.random(35-1,35-5)
Which results in
math.random(34,30)
Which is not valid since 30 is lower than 34. The second argument has to be higher in math.random()

Edited by NanoBob, 25 June 2016 - 08:23 PM.


#5 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 25 June 2016 - 08:31 PM

If you aren't sure which value will be larger, you can address that with something like this...
math.random(math.min(x - 1, x - spreadRad), math.max(x - 1, x - spreadRad))


#6 ReBraLaCC

  • Members
  • 100 posts
  • LocationSublime Text 2

Posted 26 June 2016 - 01:31 PM

View PostDog, on 25 June 2016 - 08:31 PM, said:

If you aren't sure which value will be larger, you can address that with something like this...
math.random(math.min(x - 1, x - spreadRad), math.max(x - 1, x - spreadRad))

View PostNanoBob, on 25 June 2016 - 08:21 PM, said:

View PostReBraLaCC, on 25 June 2016 - 07:46 PM, said:

View PostKingofGamesYami, on 25 June 2016 - 07:08 PM, said:

That error is caused by the second argument being less than the first argument.

thought of that, the fire code at the end is the code I am really using.... Ughhh errors that make no sense ;-;
It does in fact make sense. You call
math.random(x-1,x-spreadRad)

The first parameter, x is 35. And spreadRad (third parameter) is 5.
fire(35,19,5,5,13,6,colors.black)

So your math random is basicly
math.random(35-1,35-5)
Which results in
math.random(34,30)
Which is not valid since 30 is lower than 34. The second argument has to be higher in math.random()

Oh my gosh, you're totally right xD thanks!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users