Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Randomized Answers And Avoiding Same Responses

Randomized Answers And Avoiding Same Responses

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Dexodro   (4 posts)  Bio
Date Sat 23 Aug 2008 01:19 AM (UTC)
Message
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.
Top

Posted by Fadedparadox   USA  (91 posts)  Bio
Date Reply #1 on Sat 23 Aug 2008 02:17 AM (UTC)

Amended on Sat 23 Aug 2008 03:09 AM (UTC) by Fadedparadox

Message
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)
Top

Posted by Nick Gammon   Australia  (23,162 posts)  Bio   Forum Administrator
Date Reply #2 on Sat 23 Aug 2008 02:32 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,074 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.