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

Random rs.BundledOutput

Started by mrhat, 28 April 2014 - 12:26 PM
mrhat #1
Posted 28 April 2014 - 02:26 PM
I need something to where someone would come up, press a button, and have one of the 16 colors to be displayed. I have played around with RedNet cable from MFR and the lamps from Project Red. I just need a way to randomize this.
CometWolf #2
Posted 28 April 2014 - 03:36 PM
That should be pretty simple. Store all the color values in a numerically indexed table, then use math.random to pick a random index based on the size of the table.

local colors = {}
for k,v in pairs(color) do
  if type(v) == "number" then
    colors[#colors+1] = v
  end
end
randomColor = colors[math.random(1,#colors)]
Lyqyd #3
Posted 28 April 2014 - 03:49 PM
colors is the name of the actual API and stores more than just the color values. The better solution relies on what the color values actually are to generate a single random color:


function randomColor()
  return 2 ^ math.random(0, 15)
end
CometWolf #4
Posted 28 April 2014 - 04:06 PM
Ah right my bad, i forgot wether it was color or colors :P/>