Random Stuff, and Seeding it!

Posted by Dhai on Wed 14 Dec 2005 03:17 AM — 4 posts, 17,711 views.

#0
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!
Amended on Wed 14 Dec 2005 04:29 AM by Nick Gammon
#1
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.
#2
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 :)
Australia Forum Administrator #3
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.