[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Randomized Answers And Avoiding Same Responses

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Randomized Answers And Avoiding Same Responses

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Dexodro   (4 posts)  [Biography] bio
Date Sat 23 Aug 2008 01:19 AM (UTC)  quote  ]
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.
[Go to top] top

Posted by Fadedparadox   USA  (91 posts)  [Biography] bio
Date Reply #1 on Sat 23 Aug 2008 02:17 AM (UTC)  quote  ]

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)
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sat 23 Aug 2008 02:32 AM (UTC)  quote  ]
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
[Go to top] 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.


1,578 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]