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

Problem with non repetitive randomization

Started by SethShadowrider, 22 June 2012 - 06:17 PM
SethShadowrider #1
Posted 22 June 2012 - 08:17 PM
I am trying to create a program that randomly selects between three outputs and does not use the same output twice in a row. I got it to work a couple times but it crashes around the second or third time. I can thing of nothing that could fix it.

recent = "right"
x = math.random(1,3)
if x == 1 then
if recent == "left" then
os.reboot()
else
print("left")
recent = "left"
rs.setOutput("left", true)
sleep(1)
rs.setOutput("left", false)
os.reboot()
if x == 2 then
if recent == "middle" then
os.reboot()
else
print("middle")
recent = "middle"
rs.setOutput("back", true)
sleep(1)
rs.setOutput("back", false)
os.reboot()
end
if x == 3 then
if recent == "right" then
os.reboot()
else
print("right")
recent = "right"
rs.setOutput("right", true)
sleep(1)
rs.setOutput("right", false)
os.reboot()
end
end
end
end
end
I dont get it…
OmegaVest #2
Posted 22 June 2012 - 08:39 PM
I don't see anythign particularly wrong, except that you reboot the computer instead of using a loop.


recent = ""

while true do
   x= math.random(1,3)
   if recent then
	  if x ~= recent then
		 if x == 1 then
			outPut = "left"
		 elseif x == 2 then
			outPut = "back"
		 elseif x == 3 then
			outPut = "right"
		 else
			error("Randomizer borked")
		 end
	  print(outPut)
	  rs.setOutput(outPut, true)
	  sleep(1)
	  rs.setOutput(outPut, false)

	  else
		 print("Retrying")
	  end
   else
	  if x == 1 then
		outPut = "left"
	 elseif x == 2 then
		outPut = "back"
	 elseif x == 3 then
		outPut = "right"
	 else
		error("Randomizer borked")
	 end

	 print(outPut)
	 rs.setOutput(outPut, true)
	 sleep(1)
	 rs.setOutput(outPut, false)
   end
sleep(0.2)  -- Keeps the loop from overheating
end



This is more or less what you have, just compressed, and sort-of optimised.

Also, you will need to add a break in here somewhere, if you want to stop it. Personally, I would add a timer at the top with an os.pullEvent() and look for a keystroke, but that may not be the best way.
MysticT #3
Posted 22 June 2012 - 09:01 PM
You should use a table, it makes the code shorter and easier to change:

local tSide = { "left", "back", "right" }
local nLast = 0

while true do
  local n = math.random(#tSides)
  if n ~= nLast then
    rs.setOutput(tSides[n], true)
    sleep(1)
    rs.setOutput(tSides[n], true)
  end
end
archit #4
Posted 22 June 2012 - 09:17 PM
Just don't forget to set nLast to the current value of n after the if loop, in Mystic's code.
MysticT #5
Posted 22 June 2012 - 09:21 PM
Just don't forget to set nLast to the current value of n after the if loop, in Mystic's code.
:P/>/> Totally missed it.
Here's the fixed version:

local tSide = { "left", "back", "right" }
local nLast = 0

while true do
  local n = math.random(#tSides)
  if n ~= nLast then
    rs.setOutput(tSides[n], true)
    sleep(1)
    rs.setOutput(tSides[n], true)
    nLast = n
  end
end