random variable

I'm hoping to create a function where my alias will select random abilities from a table, I can get it to take the value from the table but not to cycle through the variables(only two right now for simplicity). What is it missing?



function mentalafflictions()

mental_affliction = {"limerick", "pastorale"}

       send("sing "..mental_affliction[1].." at "..target.."")

end


Best Answers

  • edited March 2014 Accepted Answer
      send("sing "..mental_affliction[1].." at "..target.."")

    In this line you're selecting the first entry from the table, "mental_affliction[1]"

    Try,

    send("sing "..mental_affliction[math.random(2)].." at "..target.."")

    Using the math.random, it should give you a random number between 1..2



  • NimNim
    Accepted Answer
    If you want it to be extendible to any array size, you could also use math.random(#<array>), eg. in this case

    send("sing "..mental_affliction[math.random(#mental_affliction)].." at "..target.."")

Answers

  • Crixos said:
      send("sing "..mental_affliction[1].." at "..target.."")

    In this line you're selecting the first entry from the table, "mental_affliction[1]"

    Try,

    send("sing "..mental_affliction[math.random(2)].." at "..target.."")

    Using the math.random, it should give you a random number between 1..2



    Perfect! Thank you so much.
Sign In or Register to comment.