< All Topics

Random

Experior.Core.Environment.Random

Experior.Core.Environment.Random can be used to get a pseudo random sequence of numbers.

Properties

int Seed: This property allows to get/set the Seed of the standard pseudo random number generator used by Experior.

Changing the seed allows to get a new sequence of pseudo random numbers.

Experior.Core.Mathematics.Statistics.Generators.Pseudo.StandardGenerator Generator: This property allows to get/set the standard pseudo random number generator used by Experior.

The standard pseudo random number generator has methods to obtain random bytes, integers, doubles, booleans (See example below).

Methods

void Reset(): This resets the number generator. After the reset the random number generator will generate the same sequence of numbers for the current seed.

int Random(): Returns a random integer.

int Random(int max): Returns a random integer less than the given max

int Random(int min, int max): Returns a random integer between the given min and less than the given max

 

Example:

Below is an example of the usage of a random number generator of Experior:

private void ExampleOfRandom(int seed)
{
    // Example of usage of random number generator

    // Set the seed of the pseudo random number generator
    // By using the same seed you'll get the same sequence of pseudo random numbers which fullfills
    // the reproducablility requirement (e.g. when debugging)
    // Change the seed for each iteration of experiments
    Experior.Core.Environment.Random.Seed = seed;

    // get a pseudo random integer between -20 and less than 21
    int randomint = Experior.Core.Environment.Random.Next(-20, +21);
    Experior.Core.Environment.Log.Debug.Write("Debug message " + attr1);

    Experior.Core.Environment.Random.Reset();

    // randomint will be the same value as before because we have reset the random number generator
    randomint = Experior.Core.Environment.Random.Next(-20, +21);
    Experior.Core.Environment.Log.Debug.Write("Debug message after reset seed " + attr1);

    // reset the scene (this will also reset all assemblies and reset the standard random number generator)
    Experior.Core.Environment.Scene.Reset();

    // randomint will be the same value as before because we have reset the scene
    randomint = Experior.Core.Environment.Random.Next(-20, +21);
    Experior.Core.Environment.Log.Debug.Write("Debug message after reset " + attr1);

    // You can get the standard pseudo random generator through Experior.Core.Environment.Random.Generator
    // this allows you to get random bytes, radom integers, random doubles,

    // get random double beteen 0.3 and less then 7.9
    double randomnumber = Experior.Core.Environment.Random.Generator.NextDouble(0.3, 7.9);

    byte[] randombytes = new byte[10];
    // fills the given byte array with random values
    Experior.Core.Environment.Random.Generator.NextBytes(randombytes);

    // get a random boolean value
    bool randombool = Experior.Core.Environment.Random.Generator.NextBoolean();
}
Was this article helpful?
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
Previous Mini-markup text-markup language
Next Scene