WoW:API random: Difference between revisions

demoted headings
("randseed" -> "randomseed". My bad.)
(demoted headings)
Line 1: Line 1:
{{:Lua/Libshortcut|random|math.random}}
{{:Lua/Libshortcut|random|math.random}}
=math.random and math.randomseed=
== math.random and math.randomseed ==


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


==Description==
=== Description ===
The functions math.random and math.randomseed are interfaces to the simple random generator functions rand and srand that are provided by ANSI C.  
The functions math.random and math.randomseed are interfaces to the simple random generator functions rand and srand that are provided by ANSI C.  


Line 17: Line 17:
The math.randomseed function sets a "seed" for the pseudo-random generator: Equal seeds produce equal sequences of numbers.
The math.randomseed function sets a "seed" for the pseudo-random generator: Equal seeds produce equal sequences of numbers.


==On random number generator distribution and security==
=== On random number generator distribution and security ===
The ANSI C rand() function is a Linear Congruential Pseudo-Random Number Generator (LPCRNG). Statistically speaking, LCPRNGs have a fair distribution. However, from a security standpoint they are very weak.  
The ANSI C rand() function is a Linear Congruential Pseudo-Random Number Generator (LPCRNG). Statistically speaking, LCPRNGs have a fair distribution. However, from a security standpoint they are very weak.  


Line 33: Line 33:
   math.randomseed(math.random(0,2147483647)+(GetTime()*1000));
   math.randomseed(math.random(0,2147483647)+(GetTime()*1000));


==Example==
=== Example ===
  > local x = math.random();
  > local x = math.random();
  > = x
  > = x
Line 46: Line 46:
  51 [50 - 52]
  51 [50 - 52]


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


Anonymous user