# Mathematical commands

## Description

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:

- get a random number
- round numbers
- get the square root of a number

## Getting a random number

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 )

    agk::SetRandomSeed ( 100 );

    a = agk::Random ( );

    b = agk::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.

## Round numbers

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 )

    value = agk::Round ( 1.8f );

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 )

    a = agk::Floor ( 1.8f );
    b = agk::Ceil ( 1.2f );

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.

## Square root

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 )

    value = agk::Sqrt ( 25 );

The variable value would be set to 5 as this is the square root of 25 because 5 x 5 = 25.

## Full code listing

The final code listing for this example is:

    SetRandomSeed ( 100 )

    a = Random ( )

    b = Random ( 1, 10 )

    value = Round ( 1.8 )

    a = Floor ( 1.8 )
    b = Ceil ( 1.2 )

    value = sqrt ( 25 )

    do
        Print ( a )
        Print ( b )
        Print ( value )
        Print ( a1 )
        Print ( b1 )
        Print ( value1 )
        Sync ( )
    loop

    void app::Begin ( void )
    {
        agk::SetRandomSeed ( 100 );

        a = agk::Random ( );

        b = agk::Random ( 1, 10 );

        value = agk::Round ( 1.8f );

        a = agk::Floor ( 1.8f );
        b = agk::Ceil ( 1.2f );

        value = agk::sqrt ( 25f );
    }

    void app::Loop ( void )
    {
        agk::Print ( a );
        agk::Print ( b );
        agk::Print ( value );
        agk::Print ( a1 );
        agk::Print ( b1 );
        agk::Print ( value1 );
        agk::Sync ( );
    }

## Conclusion

This example only demonstrates a small portion of the mathematical related commands. For more details please view the reference guide.