Scripts
title: Generating random values at runtime
author: VMM Team
date: 2006-09-01
description:
An infinite loop example with new random values every time your run the script.
code
proc main()
{
a;
srand(GetSystemTime());
while(1)
{
a = ((rand()%20)+50);
debug(a);
sleep(500);
}
}
/*
Explanation:
- The srand(GetSystemTime()) sets the random number generator to a random value.
If you don't do this, the first random generated value is always the same. (which is, the random value generated at compile time).
- while(1) , is always true, that's the reason the script won't terminate unless you click the stop button.
- a = ((rand()%20)+50) , calculates the modulo of a random value and 20 (thus, between 0 and 20) and adds 50 to it. This value is assigned to the variable a. Logically, the result of a is always between 50 and 70.
The following is a snippet of the results.
Every time you run the script again,
the values will be different:
ThreadId : 1 => 65.000000
ThreadId : 1 => 53.000000
ThreadId : 1 => 61.000000
ThreadId : 1 => 68.000000
ThreadId : 1 => 54.000000
ThreadId : 1 => 58.000000
ThreadId : 1 => 66.000000
ThreadId : 1 => 58.000000
*/
{
a;
srand(GetSystemTime());
while(1)
{
a = ((rand()%20)+50);
debug(a);
sleep(500);
}
}
/*
Explanation:
- The srand(GetSystemTime()) sets the random number generator to a random value.
If you don't do this, the first random generated value is always the same. (which is, the random value generated at compile time).
- while(1) , is always true, that's the reason the script won't terminate unless you click the stop button.
- a = ((rand()%20)+50) , calculates the modulo of a random value and 20 (thus, between 0 and 20) and adds 50 to it. This value is assigned to the variable a. Logically, the result of a is always between 50 and 70.
The following is a snippet of the results.
Every time you run the script again,
the values will be different:
ThreadId : 1 => 65.000000
ThreadId : 1 => 53.000000
ThreadId : 1 => 61.000000
ThreadId : 1 => 68.000000
ThreadId : 1 => 54.000000
ThreadId : 1 => 58.000000
ThreadId : 1 => 66.000000
ThreadId : 1 => 58.000000
*/
There's no sound file available yet.