world.MtRand
Returns pseudo-random number using the Mersenne Twister algorithm
Prototype
double MtRand();
Description
This returns a pseudo-random number in the range 0 to 1 (however excluding 1) as a "double" (floating-point number). Thus you can generate a number in any range by multiplying the result by the range you want.
eg.
print (math.floor (MtRand () * 5)) - returns 0 to 4
See below in the Lua section for an example of doing coin flips.
Note that the MT generator is "global" to all of MUSHclient. If you are trying to reproduce a sequence, then you cannot rely on a sequence staying stable from one script call to another (since another script, or another world or plugin, might also use random numbers).
This (Lua) script here, for example, would generate a million random numbers, and put them into a table for later use:
nums = {} -- empty table
MtSrand (1234567) -- start with a known seed
for j = 1, 1000000 do
table.insert (nums, MtRand ())
end
This took about a second to execute on my PC.
Over a run of 10,000,000 coins flips it seemed pretty accurate, giving 5,000,625 heads and 4,999,375 tails (in other words, around 50.00625% heads).
Example (in Lua) of rolling a 6-sided die:
for i = 1, 100 do
Tell (math.floor (MtRand () * 6) + 1, " ")
end
In the above example, multiplying by 6 gives a number in the range 0 to 5, and then we add 1 to make it 1 to 6.
For more details about the Mersenne Twister see:
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
VBscript example
Note MtRand
Jscript example
Note (MtRand ())
Lua example
heads = 0
tails = 0
for j = 1, 1000000 do
i = math.floor (MtRand () * 2)
if i == 0 then
heads = heads + 1
else
tails = tails + 1
end -- if
end -- for
print ("heads = ", heads) --> 498893
print ("tails = ", tails) --> 501107
Return value
A pseudo-random number in the range [0 to 1). In other words, zero might be returned, however 1 will not be.
To put it another way, the returned number is:
>= 0 and < 1
Related topic
See also
| Function | Description |
|---|---|
| MtSrand | Seed the Mersenne Twister pseudo-random number generator |