Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Pick a random integer from an array — Gideros Forum

Pick a random integer from an array

DikkesnoekDikkesnoek Member
edited February 2014 in General questions
Hey,

I was searching the web and I couldn't find an easy answer. Using arrays in Lua
is quiet different. I have an array with some integers like:

local yPositionsArray = {66, 158, 250, 342, 434, 526}

then I want to pick a random value from this array. I tried this:

local startY = math.random(#yPositionsArray)

and

local startY = math.random(1, #yPositionsArray)

None of them will work. You will get values from 1 to 6.

Thanks,

Marc

Comments

  • Try:
    local startY = yPositionsArray[math.random(1,6)]
  • Yeah, that's it. Thanks a lot.

    Regards,

    Marc
  • or basically:
    local startY = yPositionsArray[math.random(#yPositionsArray)]
Sign In or Register to comment.