This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ReBraLaCC's profile picture

[SOLVED] Problem with math.random()

Started by ReBraLaCC, 25 June 2016 - 05:21 PM
ReBraLaCC #1
Posted 25 June 2016 - 07: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 on 26 June 2016 - 03:09 PM
KingofGamesYami #2
Posted 25 June 2016 - 08:08 PM
That error is caused by the second argument being less than the first argument. For example,

math.random( 1, 0 )
ReBraLaCC #3
Posted 25 June 2016 - 08:46 PM
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 ;-;
NanoBob #4
Posted 25 June 2016 - 09:21 PM
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 on 25 June 2016 - 07:23 PM
Dog #5
Posted 25 June 2016 - 09: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))
ReBraLaCC #6
Posted 26 June 2016 - 02: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))
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!