- This wiki is out of date, use the continuation of this wiki instead
Rand
From FenixWiki
(Difference between revisions)
| Revision as of 23:23, 8 July 2007 (edit) Sandman (Talk | contribs) ← Previous diff |
Current revision (10:24, 9 May 2008) (edit) (undo) Sandman (Talk | contribs) m (→Definition) |
||
| (3 intermediate revisions not shown.) | |||
| Line 6: | Line 6: | ||
| Returns a random number, ranging from a certain lower limit to a certain upper limit. The limits are within the range. | Returns a random number, ranging from a certain lower limit to a certain upper limit. The limits are within the range. | ||
| + | |||
| + | Make sure the difference between ''lowerlimit'' and ''upperlimit'' does not exceed 32767 (2^15-1). If that is needed, the function ''rand2()'' below can be used. | ||
| == Parameters == | == Parameters == | ||
| Line 19: | Line 21: | ||
| == Notes == | == Notes == | ||
| To synchronize rand() on different computers, the function [[rand_seed]]() can be used. | To synchronize rand() on different computers, the function [[rand_seed]]() can be used. | ||
| + | |||
| + | Rand() is not a very good function on itself. To counter this, the following ''rand2()'' can be used: | ||
| + | <pre> | ||
| + | #define RAND_MAX 32767 | ||
| + | #define DRAND_RANGE (1.0/((RAND_MAX + 1)*(RAND_MAX + 1))) | ||
| + | #define irand(x) ((unsigned int) ((x) * drand ())) | ||
| + | Function float drand () | ||
| + | Private | ||
| + | float f; | ||
| + | Begin | ||
| + | Repeat | ||
| + | f = (rand (0,RAND_MAX) * (RAND_MAX + 1.0) | ||
| + | + rand (0,RAND_MAX)) * DRAND_RANGE; | ||
| + | Until (f < 1); /* Round off */ | ||
| + | return f; | ||
| + | End | ||
| + | Function int rand2(int lowerlimit, int upperlimit) | ||
| + | Begin | ||
| + | return (lowerlimit+irand(upperlimit-lowerlimit+1)); | ||
| + | End | ||
| + | </pre> | ||
| + | To understand this code, one can read [http://www.azillionmonkeys.com/qed/random.html its source]. | ||
| + | |||
| + | {{Funcbox | ||
| + | | category = Math | ||
| + | }} | ||
Current revision
Contents |
[edit] Definition
INT rand ( <INT lowerlimit> , <INT upperlimit> )
Returns a random number, ranging from a certain lower limit to a certain upper limit. The limits are within the range.
Make sure the difference between lowerlimit and upperlimit does not exceed 32767 (2^15-1). If that is needed, the function rand2() below can be used.
[edit] Parameters
| INT lowerlimit | - The lower limit for the random value. |
| INT upperlimit | - The upper limit for the random value. |
[edit] Returns
INT : A random value: lowerlimit <= result <= upperlimit
[edit] Notes
To synchronize rand() on different computers, the function rand_seed() can be used.
Rand() is not a very good function on itself. To counter this, the following rand2() can be used:
#define RAND_MAX 32767
#define DRAND_RANGE (1.0/((RAND_MAX + 1)*(RAND_MAX + 1)))
#define irand(x) ((unsigned int) ((x) * drand ()))
Function float drand ()
Private
float f;
Begin
Repeat
f = (rand (0,RAND_MAX) * (RAND_MAX + 1.0)
+ rand (0,RAND_MAX)) * DRAND_RANGE;
Until (f < 1); /* Round off */
return f;
End
Function int rand2(int lowerlimit, int upperlimit)
Begin
return (lowerlimit+irand(upperlimit-lowerlimit+1));
End
To understand this code, one can read its source.
| Math Functions | |
| • Abs() • Acos() • Asin() • Atan() • Cos() • Fget_angle() • Fget_dist() • Get_distx() • Get_disty() • Pow() • Rand() • Rand_seed() • Sin() • Sqrt() • Tan() • | |
