util.Math.rand() returns a positive pseudo-random number between 0 (zero) and the INTEGER value passed as the parameter.
Syntax
util.Math.rand(max)
Parameters
max |
an INTEGER value |
Possible values
0 to 2,147,483,646
Usage and examples
util.Math.rand() takes an INTEGER value and returns a pseudo-random INTEGER value between 0 and the method parameter:
DISPLAY util.Math.rand(100)
Numbers generated by util.Math.rand() may vary according to the operating system.
The maximum number that can be returned by util.Math.rand() function is 2,147,483,646.
If the parameter (max) is equal or lower that 0, util.Math.rand() will return 0:
example code |
MAIN DISPLAY util.Math.rand(25) CALL util.Math.srand() DISPLAY util.Math.rand(0) CALL util.Math.srand() DISPLAY util.Math.rand(-25) CALL fgl_getkey() END MAIN |
obtained results |
|
util.Math.srand() must be called before subsequently executing the program with util.Math.rand().
Otherwise, util.Math.rand() will generate the same sequence of numbers at every execution of the program:
example code #1 (run the program several times) |
MAIN DISPLAY "Before srand()" DISPLAY util.Math.rand(10) CALL util.Math.srand() DISPLAY "After srand()" DISPLAY util.Math.rand(10) CALL fgl_getkey() END MAIN |
|
obtained results #1 |
|
|
example code #2 (run the program several times) |
MAIN DISPLAY "#1 without srand()" DISPLAY util.Math.rand(10) DISPLAY"#2 without srand()" DISPLAY util.Math.rand(10) CALL fgl_getkey() END MAIN |
|
obtained results #2 |
|
|