A series of commands that deal with mathematical related functionality is included within AGK. These commands handle operations such as obtaining random numbers, square root, sine, cosine, tangent, rounding values and more. In this example several such commands are demonstrated.
In this example we'll check out commands to do the following:
There are three commands related to random numbers. These commands allow you to set a random seed (from which a random number is generated), obtain a random number and obtain a random number within a range. This example shows the process:
SetRandomSeed ( 100 ) a = Random ( ) b = Random ( 1, 10 )
The first call to SetRandomSeed passes in the value 100. This is a seed that controls how our random numbers are generated.
The second call stores a random value within the variable a. If we continued to call the Random command and stored all the values we might get a sequence like this: 56, 98, 44, 43, 75, 2. If we repeated the process the sequence would be the same. The reason behind this is related to our random seed being 100. When the seed value does not change, the same sequence of random numbers will be generated each time. By default AGK will set this seed to the current time on startup, resulting in the generation of a different sequence of numbers each time the Random function is called. So unless you require the same sequence each time there is no need to call the command SetRandomSeed.
The third call to the command Random passes in two variables. This allows us to inform the Random command that we require a number within a range. In our example the minimum value is 1 and the maximum value is 10. Calling this command numerous times might result in a sequence like this: 5, 2, 9, 1, 6, 3, 7.
Another useful feature is the ability to take a floating point number and round it to the nearest number. For example, if you had the value 1.8 and called the Round command a value of 2 would be returned e.g.
value = Round ( 1.8 )
Other options include the ability to round a number down or up. This is handled with the commands Floor and Ceil:
a = Floor ( 1.8 ) b = Ceil ( 1.2 )
In this example the variable a gets set to 1 as we're using the Floor command to round down. The variable b gets set to 2 as we're using the Ceil command to round up.
AGK also includes a command to determine the square root of a number. The square root is a value multiplied by itself that equals the number. Here's an example:
value = sqrt ( 25 )
The variable value would be set to 5 as this is the square root of 25 because 5 x 5 = 25.
The final code listing for this example is:
SetRandomSeed ( 100 ) a = Random ( ) b = Random ( 1, 10 )
value = Round ( 1.8 ) a1 = Floor ( 1.8 ) b1 = Ceil ( 1.2 ) value1 = sqrt ( 25 )
do Print ( a ) Print ( b ) Print ( value ) Print ( a1 ) Print ( b1 ) Print ( value1 ) Sync ( ) loop
This example only demonstrates a small portion of the mathematical related commands. For more details please view the reference guide.