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.
Previous Page | Page 56 of 71 | Next Page |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data