Selecting random items from a group

Posted by forral on Mon 17 May 2010 05:13 AM — 5 posts, 20,394 views.

USA #0
Hi all,

I am, once again, trying to adapt some functionalities that I had on Portal over to MUSH, and one of the major things I have missing is my rotating clan-talk tags. By "rotating" I mean that I would supply a set of strings, and it would randomly choose the strings, thus making it seem as if it is "rotating" through that list.

In Portal, the code was simply |R(item1,item2,item3) etc. What is the proper way to emulate this in MUSH?

Thank you very much for all the patience with these novice questions, and the help this forum has provided. It truly is an invaluable resource!

Sincerely,
Forral
Amended on Mon 17 May 2010 06:07 AM by forral
Australia Forum Administrator #1
To just send stuff you can do this:


stuff = {
  "sigh",
  "dance",
  "laugh",
  "sing",
  }


Send (stuff [math.random (1, #stuff)])


The first part is a table of the things you want to choose from randomly.

Then the Send indexes into the table, using math.random to get a number from 1 (the first item) to #stuff (which is the number of items in the table).
USA #2
Nick Gammon said:

To just send stuff you can do this:


stuff = {
  "sigh",
  "dance",
  "laugh",
  "sing",
  }


Send (stuff [math.random (1, #stuff)])


The first part is a table of the things you want to choose from randomly.

Then the Send indexes into the table, using math.random to get a number from 1 (the first item) to #stuff (which is the number of items in the table).



Wow Nick, thanks for the speedy response. Where would I put this though, in an alias? Send to world or script? and does it have to be called "stuff" or can I name it something like "ct"? My alias in portal was: ct
The command it executed was: clan talk |R(tag1,tag2,tag3,tag4)

*EDIT* Err, I should clarify that what I'm wanting to do is append these tags on the beginning of my clan talk alias, so if I type "ct test" it would say [X] clan members heard you say: tag1 test.....and I could keep sending that command and having random tags append before the actual test.
Amended on Mon 17 May 2010 06:58 AM by forral
Australia Forum Administrator #3
That is a (Lua) script, so it would be in send to script.

The thing called "stuff" is just the table of things you choose randomly from. The alias itself would be called whatever you want to call it.

As for the appending, instead of sending you can set up a variable, eg.


x = stuff [math.random (1, #stuff)]


Now "x" is a randomly-chosen word.

If your alias had a wildcard in it, this becomes %1, so you could do this:


Send ("clan talk " .. x .. " %1")


Or something along those lines. The .. in Lua is string concatenation.
USA #4
Nick, you are amazing. As I was just replying to ask for more help, I had an epiphany, and it works perfect. For anyone else who might read this post (since I will reference it later on), put all the code in the alias.

In my case, my alias was: ct *
And in the box I have:
stuff = {
  "|R/>|GF0rg3 |WWh0r3|R<\|C",
  "|G4|Mplay|W",
  "|M4|Rsome|Y",
  "|rMino |RSlayer|W",
  "|R-|G>|BCarter|G<|R-|W",
  }

x = stuff [math.random (1, #stuff)]

Send ("clan talk " .. x .. " %1")


Was sent to script.


Thank you very much for this help Nick. Just another reason to cement my MUSHClient usage.

Sincerely,
Forral