19. WMLScript Lang
Standard Library
WMLScript's
Lang standard library provides functions related to the WMLScript
language core. It contains functions for generating random numbers,
performing some arithmetic operations and dealing with data type
conversion.
In
the following sections, we will look at some of the more commonly
used functions in the Lang standard library.
19.1. Generating Random
Numbers: seed() and random() Functions
The
seed() and random()
functions of the Lang standard library are used to initialize the
random number generator and to generate random numbers respectively.
The syntax of the seed()
function is:
Lang.seed(numeric_value);
If
numeric_value < 0, the
value used to initialize the random number generator will be
determined by the mobile device randomly. In most cases, you will
specify the initialization value this way since it ensures the random
number sequence generated will not be repeated.
If
numeric_value >= 0,
numeric_value will be
used to initialize the random number generator.
If
no error occurs, the seed()
function returns an empty string, otherwise it returns an invalid
value.
Now
let's look at the random()
function. Its syntax is:
Lang.random(numeric_value);
The
random() function returns a random integer in the range 0 to
numeric_value if everything works fine, otherwise it returns
an invalid value.
The
following WMLScript example illustrates how to generate random
numbers:
Lang.seed(-1); var
random_num1 = Lang.random(10); var random_num2 = Lang.random(99);
After
executing the above code, random_num1
contains an integer in the range 0 to 10 and random_num2
contains an integer in the range 0 to 99.
Feedback Form ( ExpandCollapse)
|