WoW:API random: Difference between revisions

404 bytes removed ,  18 April 2009
Removed Randomseed Sorry don't know how to make the little arrow for the source
(Removed Randomseed Sorry don't know how to make the little arrow for the source)
Line 1: Line 1:
{{:Lua/Libshortcut|random|math.random}}
{{:Lua/Libshortcut|random|math.random}}
== math.random and math.randomseed ==
== math.random==


=== Usage ===
=== Usage ===
math.randomseed(s);
 
val = math.random([l, u]);
val = math.random([l, u]);


Line 24: Line 24:
   rng = rng * 214013 + 2531011
   rng = rng * 214013 + 2531011
   return (rng>>16)&0x7fff;
   return (rng>>16)&0x7fff;
'''So, how do I get numbers that are harder to guess?'''
The easiest way is to reseed the generator as often as possible with ever-changing data that third parties cannot see.
If you are not generating dozens of numbers at once, hooking an event that occurs fairly often (OnUpdate, CHAT_MSG_* events, etc) and doing something like the following to introduce entropy to your generator should work:
  math.randomseed(math.random(0,2147483647)+(GetTime()*1000));


=== Example ===
=== Example ===
Line 47: Line 39:


=== Notes ===
=== Notes ===
math.randomseed takes any kind of positive number, between 0 and 2^31-1.


The range [0,1) of math.random is exclusive, meaning that you can '''never''' get 1.0 exactly. Note that the returned number is not an integer, except the special (very rare) case of 0.
The range [0,1) of math.random is exclusive, meaning that you can '''never''' get 1.0 exactly. Note that the returned number is not an integer, except the special (very rare) case of 0.
Line 54: Line 45:


It should be noted that the reason math.random does not allow 1.0 to be returned has to do with code they implemented to avoid issues on SunOS systems. However, they "correct" this issue by using modulus division. This has a side-effect for all non-SunOS systems making 0.0 twice as likely to occur than any other randomly generated number. In other words, when it would normally roll a 1.0, Lua changes that to 0.0 - so both 0.0 and 1.0 return as 0.0.
It should be noted that the reason math.random does not allow 1.0 to be returned has to do with code they implemented to avoid issues on SunOS systems. However, they "correct" this issue by using modulus division. This has a side-effect for all non-SunOS systems making 0.0 twice as likely to occur than any other randomly generated number. In other words, when it would normally roll a 1.0, Lua changes that to 0.0 - so both 0.0 and 1.0 return as 0.0.
It has been suggested that the game client might seed the RNG when it is started
Source
http://forums.worldofwarcraft.com/thread.html?topicId=15443405133&sid=1&pageNo=10


{{LUA}}
{{LUA}}
Anonymous user