Randomized Answers And Avoiding Same Responses

Posted by Dexodro on Sat 23 Aug 2008 01:19 AM — 3 posts, 15,395 views.

#0
function gill()
local goldenseal_illusions = {
"Hmmmm. Why must everything be so difficult to figure out?",
"You look about yourself nervously.",
"You shuffle your feet noisily, suddenly bored.",
}

local gillNum = math.random(1, #goldenseal_illusions)
local gillSec = math.random(1, #goldenseal_illusions)

Send("conjure " .. misc.target .. " illusion " .. goldenseal_illusions[gillNum] .. "\\n" .. goldenseal_illusions[gillSec])
end

--

This is the randomized script, but, due to low number of choices, I get double-affliction-illusions, which would be relatively useless in combat.

How exactly might I avoid multiples? I'm rather green when it comes to Lua.
USA #1
Several possibilities come to mind. One, make a while loop that fires until they're different.


function gill()
  local goldenseal_illusions = {
    "Hmmmm. Why must everything be so difficult to figure out?",
    "You look about yourself nervously.",
    "You shuffle your feet noisily, suddenly bored.",
    }

  local gillNum = math.random(1, #goldenseal_illusions)
  local gillSec = math.random(1, #goldenseal_illusions)

  while gillSec == gillNum do
    gillSec = math.random(1, #goldenseal_illusions)
  end -- while

  Send("conjure " .. misc.target .. " illusion " .. goldenseal_illusions[gillNum] .. "\n" .. goldenseal_illusions[gillSec])
end


Another is if they're the same, increase the second by one, then make sure it isn't larger than the table. If so, make it loop around by setting it to 1 instead.

function gill()
  local goldenseal_illusions = {
    "Hmmmm. Why must everything be so difficult to figure out?",
    "You look about yourself nervously.",
    "You shuffle your feet noisily, suddenly bored.",
    }

  local gillNum = math.random(1, #goldenseal_illusions)
  local gillSec = math.random(1, #goldenseal_illusions)

  if gillSec == gillNum then
    gillSec = gillSec + 1
    if gillSec > #goldenseal_illusions then gillSec = 1 end
  end

  Send("conjure " .. misc.target .. " illusion " .. goldenseal_illusions[gillNum] .. "\n" .. goldenseal_illusions[gillSec])
end


(edited to use a while loop instead of a function in the first one)
Amended on Sat 23 Aug 2008 03:09 AM by Fadedparadox
Australia Forum Administrator #2
In your case with only 3 choices, and you want to cast 2 of them, one approach would be to use the random number generator to decide which one to exclude, and then use the other two.

However as Fadedparadox said, a more general solution is to find the first item, and then loop around getting the second one, checking each time it isn't the same as the first one.