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

startup: 26: bad argument #2: interval is empty

Started by mrpoopy345, 23 February 2014 - 06:35 AM
mrpoopy345 #1
Posted 23 February 2014 - 07:35 AM
Hello Pros! I was recently working on an AI for guess the number, and I ran into this problem.
Sometimes, the program works perfectly, and guesses the number.
But sometimes, it will do a few guesses, then error out:

startup: 26: bad argument #2: interval is empty
Current code:

h = 0
d = 1000
l = 0
more = {}
less = {}
tbg = math.random(1, 200)
 repeat
  if more[1] == nil then
   d = 20
  end
  for k, v in ipairs(more) do
   if v < d then
    d = v
   end
  end
  if less[1] == nil then
   l = 1
  end
  for k, v in ipairs(less) do
   if v > l then
    l = v
   end
 end
  l = l+1
  d = d-1
  g = math.random(l, d)
  if g > tbg then
   table.insert(more, g)
  elseif g < tbg then
   table.insert(less, g)
  end
  print(g)
  h = h+1
 until g == tbg
  print("The number was: "..tbg)
  print("It took "..h.." guesses!")
Does anyone know what is going on?
CometWolf #2
Posted 23 February 2014 - 07:50 AM
It means the first number used for math.random is higher than the second.

  d = math.max(d-1,1)
should fix it
MKlegoman357 #3
Posted 23 February 2014 - 07:59 AM
Your problem is that after some guesses 'l' becomes larger than 'd' so 'math.random' tries to get a number with an interval from, for example: 20 to 19 thus it errors.

TIP: debug your programs before you ask any questions. I just added a simple 'print("d: " .. d .. " l: " .. l)' before 'g = math.random(l, d)' to find that problem.

It means the first number used for math.random is higher than the second.

  d = math.max(d-1,1)
should fix it

Wouldn't this return even a smaller value?

EDIT: Actually that wouldn't change anything. You can also switch values given to 'math.random' but I don't think this would be what you need:


g = math.random(math.min(l, d), math.max(l, d))
Edited on 23 February 2014 - 07:04 AM
mrpoopy345 #4
Posted 23 February 2014 - 08:11 AM
Your problem is that after some guesses 'l' becomes larger than 'd' so 'math.random' tries to get a number with an interval from, for example: 20 to 19 thus it errors.

TIP: debug your programs before you ask any questions. I just added a simple 'print("d: " .. d .. " l: " .. l)' before 'g = math.random(l, d)' to find that problem.
Thanks for the help!
It seems to work now.
(And FYI I did debug it like that, but I had so many numbers on the screen I could not understand what was going on :/)
CometWolf #5
Posted 23 February 2014 - 09:04 AM
Your problem is that after some guesses 'l' becomes larger than 'd' so 'math.random' tries to get a number with an interval from, for example: 20 to 19 thus it errors.

TIP: debug your programs before you ask any questions. I just added a simple 'print("d: " .. d .. " l: " .. l)' before 'g = math.random(l, d)' to find that problem.

It means the first number used for math.random is higher than the second.

  d = math.max(d-1,1)
should fix it

Wouldn't this return even a smaller value?

EDIT: Actually that wouldn't change anything. You can also switch values given to 'math.random' but I don't think this would be what you need:


g = math.random(math.min(l, d), math.max(l, d))

  g = math.random(l, d)
Oh crap, that's an "L" lol, i thought it was a 1.