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 ➜ General ➜ Random Stuff, and Seeding it!

Random Stuff, and Seeding it!

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


Posted by Dhai   (8 posts)  Bio
Date Wed 14 Dec 2005 03:17 AM (UTC)

Amended on Wed 14 Dec 2005 04:29 AM (UTC) by Nick Gammon

Message
I've been trying to figure out how to solve this problem, but I can't quite. Help, please!

I'm converting a Zmud script to Mushclient. In the Zmud version, up to 5 variables are filled up with a value and then however many have a value are then randomized up, so that one of the things is executed.


#EXEC %item( "guard @guard1|guard @guard2|guard @guard3", %random( 1, 3))


So this randomly sends "guard @guard1", "guard @guard2", or "guard @guard3", where @guard1 through @guard 3 are variables with locations like "head" or "right arm".

Right now, in my script, I've got it set up so guard is an array, and it can have up to 5 elements with values.

I've got a few if's to check the highest element with a value, but that's where I'm stuck.

Example:

if guard[3] = "" then
  -- I'd like to randomly select between Send("guard " .. guard[1]) 
  -- and Send("guard " .. guard[2]) here, but I don't know how.
  return
end


As soon as I see how to do that, I'll be able to figure out how to make it work for all the other possibilities (between 3 random choices, 4, 5, etc).

Thanks for your time!
Top

Posted by Dhai   (8 posts)  Bio
Date Reply #1 on Wed 14 Dec 2005 03:22 AM (UTC)
Message
Also, this is how I would figure I would do it, if the MtSrand and MtRand functions worked like this.


MtSrand{1, 2, 3}
local value = MtRand()
Send("guard " .. guard[value])


But I don't think it works like this.
Top

Posted by Dhai   (8 posts)  Bio
Date Reply #2 on Wed 14 Dec 2005 04:00 AM (UTC)
Message
I've found out a way to do this, but it involves the math.random function in Lua, not MtRand or MtSrand like I was thinking I was going to be using.

Something like this:
value = math.random(min, max)
Note(table[value])

Except what I did exactly is for me and me alone :)
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Wed 14 Dec 2005 04:35 AM (UTC)
Message
As explained on this page:

http://www.gammon.com.au/scripts/doc.php?function=MtRand

you can use MtRand to generate numbers in any range by multiplying by an appropriate factor.

For example, to get a number in the range 1 to 6 you can do:


math.floor (MtRand () * 6) + 1


This is because MtRand returns a number from 0 up to but excluding 1.

However as you have discovered you can also use math.random in Lua:

http://www.gammon.com.au/scripts/doc.php?general=lua_math

Thus:


math.random (1, 6)


also returns a number in the range 1 to 6. Arguably the MtRand function generates "better" random numbers but the difference might be negligible for most cases.

Both random number generators can be seeded if you happen to want the same sequence again.

- 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,376 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.