0
if gdt.LedButton0.ButtonState
then gdt.Lcd0.Text = Random
else gdt.Lcd0.Text = Text
end

Random = ( myTable[ math.random( #myTable ) ] )

I want the code to continue to pick a random value from the table when a button is pressed. Currently, it picks a value the first time it is pressed and sticks with that value until the program is reset

I have tried looping the code, but the problem persists

2 Answers 2

1

You forgot to seed() it.

local function SomeFuncThatGetsExecutedOnceAtStartOfProgram()
    math.randomseed(os.time())
end

(where math is a local reference to the Math library and os is a local reference to the Operating System library) Then in your function:

Random = ( myTable[ math.random( #myTable ) ] )

(although, keep in mind # doesn't really return the number of items in a table)

0

Choose Random before or in your if ... then ... else ... end
Like...

-- Maybe you need to set math.randomseed() first?
-- Then...
-- math.randomseed(math.random(os.time()))

if gdt.LedButton0.ButtonState then
  Random = myTable[math.random(#myTable)]
  gdt.Lcd0.Text = Random
else
  gdt.Lcd0.Text = Text
end

Not the answer you're looking for? Browse other questions tagged or ask your own question.