Generating Random Numbers

Top  Previous  Next

Often, you may wish to generate a random number during a questionnaire.  For example, you may wish to randomly branch to one question or another.  Or, you may wish to use a random number in a mathematical function.  Lighthouse Studio uses the RandNum and SysRand functions to generate random numbers.

 


Randnum Function

 

The random number generator is seeded first by the internal respondent number (plus any seed you specify).  That keeps different respondents from receiving the same random value.  If you provide a seed value in the RandNum function, you can call the RandNum function again for that same respondent and receive the same value each time.  

 

The formats are:

 

RandNum(Seed)

This returns a random decimal value greater than or equal to 0 and less than 1.  The random values are distributed rectangularly, meaning that there is an equal probability of receiving a value between 0 and 0.1, 0.1 and 0.2, 0.3 and 0.4, etc.  The Seed must be an integer zero or greater and is used to seed the random number generator.  The seed value used is actually the internal respondent number plus the specified seed.  Therefore, if you call the RandNum function again for this respondent with the same given seed you will receive the same random value.  But, different random values are generated for different respondents.  

 

RandNum(Seed, Min#, Max#)

This returns a random integer in the range of Min# to Max#.  Using a specific seed integer is useful if you want to be able to generate the same random value later in the questionnaire. The Seed must be an integer zero or greater and is used to seed the random number generator.  The  seed value used is actually the internal respondent number plus the specified seed.  Therefore, if you call the RandNum function again for this respondent with the same given seed you will receive the same random value.  But, different random values are generated for different respondents.  

 


SysRand Function

 

The SysRand function is used for generating truly random values (not repeatable), where the random number generator is always seeded by the system clock. Caution: when respondents back up in the survey, a new random number will be chosen the next time the SysRand function is encountered.

 

The formats are:

 

SysRand( )

This returns a random decimal value greater than or equal to 0 and less than 1.  The random values are distributed rectangularly, meaning that there is an equal probability of receiving a value between 0 and 0.1, 0.1 and 0.2, 0.3 and 0.4, etc.  The seed value used is the system clock at the moment the instruction is encountered.

 

SysRand(Min#, Max#)

This returns a random integer in the range of Min# to Max#.  The seed value used is the system clock at the moment the instruction is encountered.

 

Page link: http://www.sawtoothsoftware.com/help/lighthouse-studio/manual/index.html?drawingrandomnumbers.html