Math.RandomSeed

Math.RandomSeed ( 

number Seed )

Example 1

Debug.ShowWindow(true);

Math.RandomSeed(225);
result1 = Math.Random(100);
result2 = Math.Random(100);
result3 = Math.Random(100);
Debug.Print(result1 ..", "..result2..", "..result3.."\r\n");

Math.RandomSeed(225);
result4 = Math.Random(100);
result5 = Math.Random(100);
result6 = Math.Random(100);
Debug.Print(result4 ..", "..result5..", "..result6.."\r\n");

Math.RandomSeed(5647);
result7 = Math.Random(100);
result8 = Math.Random(100);
result9 = Math.Random(100);
Debug.Print(result7 ..", "..result8..", "..result9.."\r\n");

This example seeds the random number generator with the number 225, then calls the Math.Random action three times and outputs the three random numbers to the debug window. Each time this sequence of actions is called, it produces the same sequence of random numbers. Further down in the example, we changed the seed number to 5647. You will notice this results in a different sequence of random numbers. The output from this example will be as follows:

3, 4, 32

3, 4, 32

57, 54, 24

Tip: You can copy and paste this code into the action editor to test it.

Example 2

Debug.ShowWindow(true);

seed_time = System.GetTime(TIME_FMT_SEC)..System.GetTime(TIME_FMT_MIN)..System.GetTime(TIME_FMT_HOUR);
seed_time_date = seed_time..System.GetDate(DATE_FMT_JULIAN);
seed_input = Math.Mod(seed_time_date, 2147483647);
Math.RandomSeed(seed_input);
result1 = Math.Random(100);
result2 = Math.Random(100);
result3 = Math.Random(100);
Debug.Print(result1 ..", "..result2..", "..result3.."\r\n");

seed_time = System.GetTime(TIME_FMT_SEC)..System.GetTime(TIME_FMT_MIN)..System.GetTime(TIME_FMT_HOUR);
seed_time_date = seed_time..System.GetDate(DATE_FMT_JULIAN);
seed_input = Math.Mod(seed_time_date, 2147483647);
Math.RandomSeed(seed_input);
result4 = Math.Random(100);
result5 = Math.Random(100);
result6 = Math.Random(100);
Debug.Print(result4 ..", "..result5..", "..result6.."\r\n");

Math.RandomSeed(5647);
result7 = Math.Random(100);
result8 = Math.Random(100);
result9 = Math.Random(100);
Debug.Print(result7 ..", "..result8..", "..result9.."\r\n");

In the first part of this example, the random number generator is seeded with the modulus of a number composed of the current seconds, minutes, hours and date (in Julian format), and the max seed number 2147483647. Using this method ensures that the seed value is always unique, resulting in a random sequence of numbers. However in the last part of the example, the seed value is always the same, therefore it's result will always be the same value.

Tip: You can copy and paste this code into the action editor to test it.

See also:  Related Actions