WoW:API random: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (cat)
Line 2: Line 2:


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


Line 19: Line 19:
  > local x = math.random();
  > local x = math.random();
  > = x
  > = x
  0.34534 (0 - 1)
  0.34534 [0 - 1[


  > local x = math.random(100);
  > local x = math.random(100);
  > = x
  > = x
  53 (1 - 100)
  53 [1 - 100]


  > local x = math.random(50, 52);
  > local x = math.random(50, 52);
  51 (50 - 52)
> = x
  51 [50 - 52]


==Notes==
==Notes==
At this stage, I don't know if the range is inclusive or exclusive. [[User:Zingfharn|Zingfharn]]
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.


One would suppose through deduction that, since random 1 3 will indeed give you numbers other than 2, that the other types of "random" would also give you  inclusive results.  This is not guaranteed of course, but it seems like the developers would be consistent in their coding. [[User:Disco|Disco]]
The other ranges of the functions are inclusive, and will always return integers.


{{Template:WoW API}}
{{Template:WoW API}}

Revision as of 04:59, 6 January 2006

math.random and math.randomseed

Usage

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

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. (No guarantees can be given for their statistical properties.)

When called without arguments, math.random returns a pseudo-random real number in the range [0,1).

When called with a number n, math.random returns a pseudo-random integer in the range [1,n].

When called with two arguments, l and u, math.random returns a pseudo-random integer in the range [l,u].

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

Example

> local x = math.random();
> = x
0.34534 [0 - 1[
> local x = math.random(100);
> = x
53 [1 - 100]
> local x = math.random(50, 52);
> = x
51 [50 - 52]

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 other ranges of the functions are inclusive, and will always return integers.

Template:WoW API