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

help with number gernerator

Started by JamiePhonic, 23 September 2012 - 10:50 AM
JamiePhonic #1
Posted 23 September 2012 - 12:50 PM
i'm trying to write a program that will use bundled cables in conjunction with coloured lamps to make what are basically disco lights, the program should generate a random number between 1 and 16 and then depending on that number make a certain light or group on lights flash

heres what i got so far.. (it only goes up to 10 for now because the code has been copied and re-purposed from another program)

val = math.random(0,10)
sleep (1)
while true do
  
   if val == "1" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.brown)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.brown)
   end
  
   if val == "2" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.white)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.white)
   end
  
   if val == "3" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.blue)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.blue)
   end
  
   if val == "4" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.lightBlue)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.lightBlue)
   end
   if val == "5" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.pink)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.pink)
   end
  
   if val == "6" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.orange)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.orange)
   end
  
   if val == "7" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.yellow)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.yellow)
   end
  
   if val == "8" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.purple)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.purple)
   end
  
   if val == "9" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.red)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.red)
   end
  
   if val == "10" then
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.green)
   sleep(1)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.green)
   end
end

right now, the only thing this program does is cause CPU usage to rocket to 100% and the only way to stop it is to kill the program then restart minecraft (tekkit)
any help is greatly appreciated.
Zoinky #2
Posted 23 September 2012 - 12:57 PM
You need to add sleep(1) after each rs.setBundleOutput(). Including the ones before the 'end'.
Edit: Btw, you'll need the 'val = math.random(1,10)' after the 'while true do'. That'll make the lights change.
sjele #3
Posted 23 September 2012 - 02:20 PM
You are better of by using if/elseif's like this:


if something == "this" then
print("Guess what!!! I am this")
elseif something == "That" then
print("Woow i am that")
elseif something == "dat" then
print("Woow i am a .dat file")
end
JamiePhonic #4
Posted 23 September 2012 - 02:35 PM
ok. code is now


term.clear()
term.setCursorPos(1,1)
print ("HOLD CTRL+R TO QUIT")
sleep(2)
term.clear()
term.setCursorPos(1,1)

while true do

   val = math.random(1,11)
   time = math.random(1,2)
   sleep(0.25)
   if val == 1 then
   print (val, "-Brown", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.brown)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.brown)
   elseif val == 2 then
   print (val, "-Blue", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.blue)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.blue)
   elseif val == 3 then
   print (val, "-Light Blue", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.lightBlue)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.lightBlue)
   elseif val == 4 then
   print (val, "-Pink", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.pink)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.pink)
   elseif val == 5 then
   print (val, "-Orange", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.orange)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.orange)
   elseif val == 6 then
   print (val, "-Yellow", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.yellow)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.yellow)
   elseif val == 7 then
   print (val, "-Purple", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.purple)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.purple)
   elseif val == 8 then
   print (val, "-Red", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.red)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.red)
   elseif val == 9 then
   print (val, "-Green", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.green)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.green)
   elseif val == 10 then
   print (val, "-Grey", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.grey)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.grey)
   elseif val == 11 then
   print (val, "-white", " ", time, " Seconds")
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.white)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.white)
   end
end

it now works. but is there a way to make math.random generate random deciamal numbers like 0.3, 0.5 (for the time variable) and so on….
MysticT #5
Posted 23 September 2012 - 03:44 PM
it now works. but is there a way to make math.random generate random deciamal numbers like 0.3, 0.5 (for the time variable) and so on….
Calling math.random without arguments returns a value between 0 and 1.
Also, you can use tables to shorten your code to a few lines:

local sSide = "back"
local tColors = { colors.brown, colors.blue, colors.lightBlue, colors.pink, colors.orange, colors.yellow, colors.purple, colors.red, colors.green, colors.gray, colors.white }

while true do
  local c = tColors[math.random(1, #tColors)] -- get a random color from the table
  local time = math.random() + 1 -- random number between 1 and 2
  local output = rs.getBundledOutput(sSide)
  rs.setBundledOutput(colors.combine(output, c))
  sleep(time)
  rs.setBundledOutput(output)
end
Anonomit #6
Posted 23 September 2012 - 06:41 PM
Good job MysticT.
You forgot the side parameter in rs.setBundledOutput()
This version will choose a random color every 0.25 seconds and toggle it.

local sSide = "back"
local tColors = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black }

while true do
  local c = tColors[math.random(1, #tColors)]
  local time = 0.25
  local output = rs.getBundledOutput(sSide)

  if colors.test(output, c) then
    output = colors.subtract(output, c)

  else

   output = colors.combine(output, c)

  end
  rs.setBundledOutput(sSide, output )
  sleep(time)
end
MysticT #7
Posted 23 September 2012 - 06:53 PM
Good job MysticT.
You forgot the side parameter in rs.setBundledOutput()
This version will choose a random color every 0.25 seconds and toggle it.

local sSide = "back"
local tColors = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black }

while true do
  local c = tColors[math.random(1, #tColors)]
  local time = 0.25
  local output = rs.getBundledOutput(sSide)

  if colors.test(output, c) then
	output = colors.subtract(output, c)

  else

   output = colors.combine(output, c)

  end
  rs.setBundledOutput(sSide, output )
  sleep(time)
end
Missed that :P/>/>
Here's another version, with random time and somewhat improved toggling:

local sSide = "back"
local tColors = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black }

while true do
  local c = tColors[math.random(1, #tColors)]
  local time = math.random() + 1
  local output = rs.getBundledOutput(sSide)
  rs.setBundledOutput(sSide, bit.bxor(output, c)) -- xor will toggle the given color bit
  sleep(time)
end
JamiePhonic #8
Posted 24 September 2012 - 10:10 AM
Good job MysticT.
You forgot the side parameter in rs.setBundledOutput()
This version will choose a random color every 0.25 seconds and toggle it.

local sSide = "back"
local tColors = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black }

while true do
  local c = tColors[math.random(1, #tColors)]
  local time = 0.25
  local output = rs.getBundledOutput(sSide)

  if colors.test(output, c) then
	output = colors.subtract(output, c)

  else

   output = colors.combine(output, c)

  end
  rs.setBundledOutput(sSide, output )
  sleep(time)
end
Missed that :P/>/>
Here's another version, with random time and somewhat improved toggling:

local sSide = "back"
local tColors = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.gray, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black }

while true do
  local c = tColors[math.random(1, #tColors)]
  local time = math.random() + 1
  local output = rs.getBundledOutput(sSide)
  rs.setBundledOutput(sSide, bit.bxor(output, c)) -- xor will toggle the given color bit
  sleep(time)
end

while this code works, i need the lights to flash and overlap (e.g. multiple lights on at one time) not toggle. i also need the code to show the output like it does here:

   if val == 1 then
   print (val, "-Brown", " ", time, " Seconds")  <<====
   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.brown)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.brown)
Luanub #9
Posted 24 September 2012 - 12:57 PM

local c = 0 -- add this to the top

c = colors.combine(c, colors.red) -- adds red
rs.setBundledOutput(sSide, c ) -- red will turn on

c = colors.combine(c, colors.blue) --adds blue
rs.setBundledOutput(sSide, c ) -- blue turns on leaving red on as well

c = colors.subtract(c, colors.red) -- removes red
rs.setBundledOutput(sSide, c ) -- turns red off leaving blue on
JamiePhonic #10
Posted 24 September 2012 - 01:32 PM

local c = 0 -- add this to the top

c = colors.combine(c, colors.red) -- adds red
rs.setBundledOutput(sSide, c ) -- red will turn on

c = colors.combine(c, colors.blue) --adds blue
rs.setBundledOutput(sSide, c ) -- blue turns on leaving red on as well

c = colors.subtract(c, colors.red) -- removes red
rs.setBundledOutput(sSide, c ) -- turns red off leaving blue on

gives: bad argument: string expected, got nill

EDIT: my bad. i put second part of code in the wrong place.
code is now:

local c = 0
local sSide = "back"
local tColors = {colors.yellow, colors.brown, colors.orange, colors.red, colors.blue, colors.lightBlue, colors.green, colors.lime}
while true do
  local c = tColors[math.random(1, #tColors)]
  local time = math.random() + 1
  local output = rs.getBundledOutput(sSide)
  c = colors.combine(c, colors.red) -- adds red
  rs.setBundledOutput(sSide, c ) -- red will turn on
  c = colors.combine(c, colors.brown) --adds brown
  rs.setBundledOutput(sSide, c ) -- brown turns on leaving red on as well
  c = colors.subtract(c, colors.red) -- removes red
  rs.setBundledOutput(sSide, c ) -- turns red off leaving brown on
  sleep(time)
end
but it still doesn't do what i need it to. (only need 8 colours now BTW)
i need it to flash random lights for a random time and flash more that one at a time and also display on the monitor, which colors were on and for how long. the second set of code i posted (post #4) did that bar having more than one light on at a time.
Luanub #11
Posted 24 September 2012 - 09:53 PM
You're doing pretty much the same thing only in a way that can produce errors, and you missed a sleep.

So for post #4 or really any of them replace this type of stuff

   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.blue)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.blue)


With something like

c = colors.combine(c, colors.blue)
rs.setBundledOutput("back", c )
sleep(time)
c = colors.subtract(c, colors.blue)
rs.setBundledOutput("back", c )
sleep(time)

The 2nd sleep is very important. If the amount of time your sleeping is important then split it between the two or make the 2nd one really small.
JamiePhonic #12
Posted 24 September 2012 - 10:19 PM
You're doing pretty much the same thing only in a way that can produce errors, and you missed a sleep.

So for post #4 or really any of them replace this type of stuff

   rs.setBundledOutput("back", rs.getBundledOutput("back") + colours.blue)
   sleep(time)
   rs.setBundledOutput("back", rs.getBundledOutput("back") - colours.blue)


With something like

c = colors.combine(c, colors.blue)
rs.setBundledOutput("back", c )
sleep(time)
c = colors.subtract(c, colors.blue)
rs.setBundledOutput("back", c )
sleep(time)

The 2nd sleep is very important. If the amount of time your sleeping is important then split it between the two or make the 2nd one really small.

the code now looks like:

term.clear()
term.setCursorPos(1,1)
print ("HOLD CTRL+R TO QUIT")
sleep(2)
term.clear()
term.setCursorPos(1,1)
local c = 0
while true do
  
   val = math.random(1,8)
   time = (math.random(2,10))/10
   sleep(0.25)
  
   if val == 1 then
   print (val, "-Brown", " ", time, " Seconds")
   c = colors.combine(c, colors.brown)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.brown)
   rs.setBundledOutput("back", c )
   sleep(time)
  
   elseif val == 2 then
   print (val, "-Blue", " ", time, " Seconds")
   c = colors.combine(c, colors.blue)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.blue)
   rs.setBundledOutput("back", c )
   sleep(time)
  
   elseif val == 3 then
   print (val, "-Light Blue", " ", time, " Seconds")
   c = colors.combine(c, colors.lightBlue)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.lightBlue)
   rs.setBundledOutput("back", c )
   sleep(time)
   elseif val == 4 then
   print (val, "-Orange", " ", time, " Seconds")
   c = colors.combine(c, colors.orange)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.orange)
   rs.setBundledOutput("back", c )
   sleep(time)
  
   elseif val == 5 then
   print (val, "-Yellow", " ", time, " Seconds")
   c = colors.combine(c, colors.yellow)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.yellow)
   rs.setBundledOutput("back", c )
   sleep(time)
  
   elseif val == 6 then
   print (val, "-Red", " ", time, " Seconds")
   c = colors.combine(c, colors.red)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.red)
   rs.setBundledOutput("back", c )
   sleep(time)
  
   elseif val == 7 then
   print (val, "-Green", " ", time, " Seconds")
   c = colors.combine(c, colors.green)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.green)
   rs.setBundledOutput("back", c )
   sleep(time)
  
   elseif val == 8 then
   print (val, "-lime", " ", time, " Seconds")
   c = colors.combine(c, colors.lime)
   rs.setBundledOutput("back", c )
   sleep(time)
   c = colors.subtract(c, colors.lime)
   rs.setBundledOutput("back", c )
   sleep(time)
   end
end
but its still not working. can someone please just show me what to do and make things simple.
Luanub #13
Posted 24 September 2012 - 10:22 PM
Your prints are wrong sorry I didn't look at it earlier as it sounded like it was working.


--replace
print (val, "-lime", " ", time, " Seconds")
--with
print(val.." -lime "..time.." Seconds")
Anonomit #14
Posted 24 September 2012 - 10:24 PM
I hope this is what you're looking for. You'll need a monitor setup at least 4 wide and 2 high to see the complete display. Change bundledSide to whatever side the bundled cable is attached to. You can also change the random numbers that are generated by changing multOn, addOn, multOff and addOff. The maximum random number will be max + add, and the minimum random number will be add. You can configure the random time the wires will be powered on and off separately. Do not set the mult to 0.
Here is the code:

Spoiler


local bundledSide = "back"   --change to side bundled wires are attached to

--random time wires are on = math.random( addOn, multOn+addOn )
local multOn = 2
local addOn = 0.25

--random times wires are off = math.random( addOff, multOff+addOff )
local multOff = 1.5
local addOff = 0.25


local function randomOn()
	return math.random() * multOn + addOn
end --local function random()


local function randomOff()
	return math.random() * multOff + addOff
end --local function randomOff()


local color = { "Yellow", "Brown", "Orange", "Red", "Blue", "LightBlue", "Green", "Lime" }

rs.setBundledOutput( bundledSide, 0 )
term.clear()
term.setCursorPos( 1, 1 )
write( "HOLD CTRL-R TO REBOOT" )


local function round( number, dp)
	
	local multiple = 10^( dp or 0 )
	
	if #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 4 then
		return tostring( math.floor( number * multiple + 0.5 ) / multiple )
		
	elseif #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 3 then
		return tostring( math.floor( number * multiple + 0.5 ) / multiple .. "0" )
		
	elseif #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 1 then
		return tostring( math.floor( number * multiple + 0.5 ) / multiple .. ".00" )
		
	end --if #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 4
	
end --local function round( number, dp )


local function display( message, bit )
	
	if message == nil or bit == nil then
		print( "Error while printing to monitor" )
		sleep( 2 )
		print( "Press any key to continue" )
		os.pullEvent( "key" )
		os.shutdown()
	end --if message == nil or bit == nil
	
	local side = "top"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "bottom"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "right"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "left"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "back"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	
end --local function display( message, line )


local function bit1()
	
	local bit = 1
	local value = 16
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit1()


local function bit2()
	
	local bit = 2
	local value = 2
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit2()


local function bit3()
	
	local bit = 3
	local value = 4096
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit3()


local function bit4()
	
	local bit = 4
	local value = 16384
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit4()


local function bit5()
	
	local bit = 5
	local value = 2048
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit5()


local function bit6()
	
	local bit = 6
	local value = 8
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit6()


local function bit7()
	
	local bit = 7
	local value = 8192
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit7()


local function bit8()
	
	local bit = 8
	local value = 32
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit8()


while true do
	parallel.waitForAny( bit1, bit2, bit3, bit4, bit5, bit6, bit7, bit8 )
end --while true

MysticT #15
Posted 24 September 2012 - 10:24 PM
Your prints are wrong sorry I didn't look at it earlier as it sounded like it was working.


--replace
print (val, "-lime", " ", time, " Seconds")
--with
print(val.." -lime "..time.." Seconds")
No they are not, print will print every variable you pass as an argument, you don't need to cancatenate them. (Don't know why most people don't know this)

Edit: btw, the code I gave you should work as you want. Only that you need to put the combined colors inside the table if you want to toggle them (turn on or off) at the same time. Like this:

local sSide = "back"
local tColors = { colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.combine(colors.white, colors.orange), colors.combine(colors.magenta, colors.lightBlue) }

while true do
  local c = tColors[math.random(1, #tColors)]
  local time = math.random() + 1
  local output = rs.getBundledOutput(sSide)
  rs.setBundledOutput(sSide, bit.bxor(output, c)) -- xor will toggle the given color bit
  sleep(time)
end
Anonomit #16
Posted 24 September 2012 - 10:28 PM
Your prints are wrong sorry I didn't look at it earlier as it sounded like it was working.


--replace
print (val, "-lime", " ", time, " Seconds")
--with
print(val.." -lime "..time.." Seconds")
No they are not, print will print every variable you pass as an argument, you don't need to cancatenate them. (Don't know why most people don't know this)

True. If i have a lot of arguments in my print call, I find it's easier to follow if I print with commas.
JamiePhonic #17
Posted 24 September 2012 - 11:06 PM
I hope this is what you're looking for. You'll need a monitor setup at least 4 wide and 2 high to see the complete display. Change bundledSide to whatever side the bundled cable is attached to. You can also change the random numbers that are generated by changing multOn, addOn, multOff and addOff. The maximum random number will be max + add, and the minimum random number will be add. You can configure the random time the wires will be powered on and off separately. Do not set the mult to 0.
Here is the code:

Spoiler


local bundledSide = "back"   --change to side bundled wires are attached to

--random time wires are on = math.random( addOn, multOn+addOn )
local multOn = 2
local addOn = 0.25

--random times wires are off = math.random( addOff, multOff+addOff )
local multOff = 1.5
local addOff = 0.25


local function randomOn()
	return math.random() * multOn + addOn
end --local function random()


local function randomOff()
	return math.random() * multOff + addOff
end --local function randomOff()


local color = { "Yellow", "Brown", "Orange", "Red", "Blue", "LightBlue", "Green", "Lime" }

rs.setBundledOutput( bundledSide, 0 )
term.clear()
term.setCursorPos( 1, 1 )
write( "HOLD CTRL-R TO REBOOT" )


local function round( number, dp)
	
	local multiple = 10^( dp or 0 )
	
	if #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 4 then
		return tostring( math.floor( number * multiple + 0.5 ) / multiple )
		
	elseif #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 3 then
		return tostring( math.floor( number * multiple + 0.5 ) / multiple .. "0" )
		
	elseif #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 1 then
		return tostring( math.floor( number * multiple + 0.5 ) / multiple .. ".00" )
		
	end --if #( tostring( math.floor( number * multiple + 0.5 ) / multiple ) ) == 4
	
end --local function round( number, dp )


local function display( message, bit )
	
	if message == nil or bit == nil then
		print( "Error while printing to monitor" )
		sleep( 2 )
		print( "Press any key to continue" )
		os.pullEvent( "key" )
		os.shutdown()
	end --if message == nil or bit == nil
	
	local side = "top"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "bottom"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "right"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "left"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	local side = "back"
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor" then
		local mon = peripheral.wrap( side )
		mon.setCursorPos( 1, bit )
		mon.clearLine()
		mon.write( color[bit] .. ":" )
		mon.setCursorPos( 15, bit )
		mon.write( message )
	end --if peripheral.isPresent( side ) and peripheral.getType( side ) == "monitor"
	
	
end --local function display( message, line )


local function bit1()
	
	local bit = 1
	local value = 16
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit1()


local function bit2()
	
	local bit = 2
	local value = 2
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit2()


local function bit3()
	
	local bit = 3
	local value = 4096
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit3()


local function bit4()
	
	local bit = 4
	local value = 16384
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit4()


local function bit5()
	
	local bit = 5
	local value = 2048
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit5()


local function bit6()
	
	local bit = 6
	local value = 8
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit6()


local function bit7()
	
	local bit = 7
	local value = 8192
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit7()


local function bit8()
	
	local bit = 8
	local value = 32
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit8()


while true do
	parallel.waitForAny( bit1, bit2, bit3, bit4, bit5, bit6, bit7, bit8 )
end --while true


THANK YOU SO MUCH!!! THIS CODE IS PERFECT!

could you post instructions on how to expand the code to include more that 8 colours?
Anonomit #18
Posted 25 September 2012 - 04:17 PM
Sure. The first thing you'll need to do is define more colors. On line 23, by my count, I define color as an array. Add any additional colors on the end of the array. Make sure they're capitalised and in quotes, like the others.
Next, you'll need to define more functions. I've made it in a way that makes it is very easy to do this. Simply copy a function that starts with 'bit', (ie. bit1() ) and paste it near the bottom, before the while loop. Rename it to whatever you want, preferably bit9() or bit10(), depending on how many more colors you're adding. Inside the function, there are only 2 lines that you need to change, those are the first 2:
Spoiler

local bit = 1
local value = 16
Think of the bit variable as a variable to remember the order of the colors. You'll notice that it increases by one for each function. So make it 9. If you need to add more colors, make it 10 on the next function, 11 on the one after, so on. The next variable is a little more tricky. It's the value variable. Remember when we defined color at the top? The order really does matter. The first value in the array is "Yellow". If we look at the order the wools are in on the NEI panel, yellow is the fifth color. In binary, yellow is worth 16. For easy reference, here are the values of all of the colors, in their correct NEI order:
Spoiler

white = 1
orange = 2
magenta = 4
lightBlue = 8
yellow = 16
lime = 32
pink = 64
gray = 128
lightGray = 256
cyan = 512
purple = 1024
blue = 2048
brown = 4096
green = 8192
red = 16384
black = 32768
For this to work correctly, the value you assign will need to match with the color in the 'bit' spot in the array. To simplify that, the fourth color in the 'color' array is "Red". So, in the function bit4():
Spoiler

local bit = 4
local value = 16384
'bit = 4' points to the fourth spot in the array, and 'value = 16384' tells the program that the color has a value of 16384, so the fourth color on the array is red.

Almost done. As a last step, you'll find the 'parallel.waitForAny()' command at the bottom of the program. The arguments are functions. Simply add in, in any order, the functions you added to the program. Note that they are arguments, so don't include the parentheses on the functions
Spoiler

parallel.waitForAny( bit1, bit2, bit3 )  --like this
parallel.waitForAny( bit1(), bit2(), bit3() ) --not like this
Finally, as a example, if you were to add the color cyan, it would look like this:
Spoiler


--at the top this would be the array
local color = { "Yellow", "Brown", "Orange", "Red", "Blue", "LightBlue", "Green", "Lime", "Cyan" }



local function bit9()
	
	local bit = 9   --this is 9 because cyan is the ninth color
	local value = 512   --this is 512 because the value of cyan is 512
	
	display( "Currently  OFF", bit )
	sleep( randomOff() )
	
	while true do
		rs.setBundledOutput( bundledSide, colors.combine( rs.getBundledOutput( bundledSide ), value ) )
		local time = randomOn()
		display( "Currently  ON for " .. round( time, 2 ) .. "s", bit )
		sleep( time )
		rs.setBundledOutput( bundledSide, colors.subtract( rs.getBundledOutput( bundledSide ), value ) )
		display( "Previously ON for " .. round( time, 2 ) .. "s", bit )
		local time = randomOff()
		sleep( time )
	end --while true
	
end --local function bit9()


--at the bottom, you should see this:
while true do
	parallel.waitForAny( bit1, bit2, bit3, bit4, bit5, bit6, bit7, bit8, bit9 )  --bit9 has been included, as it is the name of the function I created for cyan
end --while true


I haven't tested the above code, but if you follow my instructions, it should should work out properly.

I hope I helped!
Doyle3694 #19
Posted 25 September 2012 - 07:15 PM
Why don't you:?

rs.setBundledOutput("back", math.random(1, 65535))
Cranium #20
Posted 25 September 2012 - 07:26 PM
Why don't you:?

rs.setBundledOutput("back", math.random(1, 65535))
Because for two reasons:
  1. math.random will generate every possible number between 1 and 65535, and there are only 16 that we can use.
  2. Try running a random program that will calculate any number within that parameter, and you will lag the computer to hell.
Doyle3694 #21
Posted 25 September 2012 - 07:31 PM
we can use 65535… it's all the numbers that can be located on 16 bits, but the lag reason is more of a reason I understand…
Cranium #22
Posted 25 September 2012 - 07:36 PM
We CAN'T use math.random(1,65535) for the main reason that you would be calculating a random number between 1 and 65,535. That means it has a very small chance of hitting any one of the 16 useable numbers for the bundled cable output. So the program would be waiting quite a long time before it would hit RANDOMLY on one of those 16 very specific numbers. I challenge you to create a program that would print an output anytime math.random(1,65535) would hit on any one of the numbers used for the Redstone API. It would take a long time to reach any of those numbers.
Anonomit #23
Posted 25 September 2012 - 07:44 PM
Why don't you:?

rs.setBundledOutput("back", math.random(1, 65535))
Because for two reasons:
  1. math.random will generate every possible number between 1 and 65535, and there are only 16 that we can use.
  2. Try running a random program that will calculate any number within that parameter, and you will lag the computer to hell.

Also, I want to display the random number on a monitor. I did this by calling a function that returns a string that's four characters long. This way, the number always takes up the same amount of space on the screen, so long as the number chosen is below 10 and not negative.
Doyle3694 #24
Posted 25 September 2012 - 07:52 PM
We CAN'T use math.random(1,65535) for the main reason that you would be calculating a random number between 1 and 65,535. That means it has a very small chance of hitting any one of the 16 useable numbers for the bundled cable output. So the program would be waiting quite a long time before it would hit RANDOMLY on one of those 16 very specific numbers. I challenge you to create a program that would print an output anytime math.random(1,65535) would hit on any one of the numbers used for the Redstone API. It would take a long time to reach any of those numbers.

well no, 65535 would be all colors.
Cranium #25
Posted 25 September 2012 - 08:01 PM
well no, 65535 would be all colors.
Did you read what I wrote? Yes, 65535 would be all colors. BUT, with math.random, it would randomly select ALL NUMBERS WITHIN THAT RANGE. You would have next to no chance of finding every combination of colors. If I did my math right, there are a total of 256 possible combinations of colors within 65,535 DIFFERENT numbers. That is a 0.39% chance of hitting upon one of those specific combinations each random pass. Adding such a huge amount to math.random is never a good idea unles you have a very good reason to. For this application, we would instead want something like several iterations of math.random(1,16). Theb compare the results with existing color numbers or codes with the redstone API. Much easier. Period.
GopherAtl #26
Posted 25 September 2012 - 08:10 PM
cranium, calm down and back up. You're right but you're not making it at all clear why you're right, I had to go read page one to see the original problem to understand myself.

Doyle, They don't want random values for all 16 wires, they want to randomly activate exactly one of the 16 wires.
Cranium #27
Posted 25 September 2012 - 08:21 PM
Sorry, got a little heated there, huh?
But yeah, Doyle, what we want is this:

local function colorConvert()
local random = math.random(1,16)
if random > 1 then return 2^(random - 2)
else return 1
end
This makes the random amount from 1 to 16, and instantly converts it to the appropriate color code in decimal format.
It would be called back like this:

rs.setBundledOutput("side",colorConvert())
That would randomly select one of 16 colors, and set it to that.
MysticT #28
Posted 25 September 2012 - 11:25 PM
Well, if you only want one color at a time, then Cranium is (kind of) right.
But what Doyle said is correct. Colors in bundled cables are handled in 16 bits (1 bit for each color -> 16 colors), so when a color is on the bit is 1, when it's off it's 0. Now when you have more that one color on, there will be more than one 1 bit. So the number of possible combinations is 2^16, wich is 65536 (from 0 to 65535). Now if you generate a random number between 1 and 65535, you'll get a random combination of colors.
If you want to use the 16 colors, this way is a lot easier.
Also, who says that generating a high random number will cause lag? Try using math.random(1, 4294967296), it will take no time.
Cranium #29
Posted 25 September 2012 - 11:28 PM
Causes lag for me…..something wrong with my setup then?

Edit: Maybe it's becuase I have only used large numbers in an infinite loop.
Edited on 25 September 2012 - 09:28 PM
MysticT #30
Posted 25 September 2012 - 11:38 PM
Causes lag for me…..something wrong with my setup then?

Edit: Maybe it's becuase I have only used large numbers in an infinite loop.
I just tested, it works fine. The only thing is that it uses 32-bit signed integers, so the maximum value is 2147483647. But it generated 100000 numbers in ~0.05 seconds. Used this code:

local t0 = os.clock()
for i = 1, 100000 do
  math.random(2147483647)
end
print(os.clock() - t0)
Cranium #31
Posted 25 September 2012 - 11:59 PM
I'm not at my minecraft or CCemu right now, but I have never done it with large numbers in a for loop. Just in a while true do loop.

while true do
local number = math.random(1,2147483647)
print(number)
sleep(0) --if I change it, then it goes too slow.
end
Not sure if this is a bad idea, but that's an example I have used before. Lag city…..
Adding sleep(1) fixes it though…so maybe I am just being a stupid programmer… :P/>/>
MysticT #32
Posted 26 September 2012 - 12:32 AM
I'm not at my minecraft or CCemu right now, but I have never done it with large numbers in a for loop. Just in a while true do loop.

while true do
local number = math.random(1,2147483647)
print(number)
sleep(0) --if I change it, then it goes too slow.
end
Not sure if this is a bad idea, but that's an example I have used before. Lag city…..
Adding sleep(1) fixes it though…so maybe I am just being a stupid programmer… :P/>/>
Well, that doesn't cause lag neither. But if it did, the problem is not the math.random, it's the loop. It's an infinite loop that yields for almost no time, it will obviously cause some lag.
Cranium #33
Posted 26 September 2012 - 12:33 AM
Then I'm afraid I am being a stupid programmer… :P/>/>
Doyle3694 #34
Posted 26 September 2012 - 07:31 PM
So math.random (1, 65535) works then? or doesn't it? I'm abit confused, I'm just some average programer, no pro by all means.
MysticT #35
Posted 26 September 2012 - 07:38 PM
Yes, it works.