RanSequence
This function returns an array containing values selected randomly from a given series of integers.
Syntax
RanSequence(<start>, <end> [, <step> [, <count>[, <seed>]]])
Parameters
<start>
Type: Long
The first possible value.
<end>
The last possible value.
Type: Long
<step>
(Optional.) The increment between consecutive possible values. The default value is 1.
Type: Long
<count>
Type: Long
(Optional.) The number of items to return. If omitted, negative, or greater than the number of possible values, all items are returned.
<seed>
Type: Long
(Optional.) The starting point to be used for the random number generator. The default value is 0.
(return)
Type: None
An array containing a random selection from the set of possible numbers.
Notes
The RanSequence function returns an array containing numbers from the sequence <start>, <start> + <step>, <start> + <step> * 2, ... , <start> + <step> * N (where N is the maximum integer such that the sequence doesn't pass <end>), in a random order. <step> can be positive or negative.
If Step is 0, <count> copies of <start> are returned, unless <count> is omitted or negative, in which case an empty array is returned. If <step> is positive and <start> is greater than End, or if <step> is negative and <start> is less than End, an empty array is returned.
The <start>, <end>, and <step> arguments define the potential set of numbers to return (for example, a setting of (1, 10, 2) produces the set {1, 3, 5, 7, 9}, while (10, 1, -2) produces {10, 8, 6, 4, 2}). Then the Seed and the starting point for random number generation determine in which order those numbers would be returned. Finally, the <count> argument controls how much of that ordered set is returned. Provided <step> is not set to 0, the returned numbers are guaranteed to be unique, so no number occurs more than once in the array.
If
<seed> is non-zero, it is used to reset the starting point for the random number generator, as in
SetRandomSeed, before the randomization.
If <seed> is not supplied or has a value of 0, the function uses the starting point stored for the random number generator by the mrScriptBasic engine or Evaluate component and passed as a hidden parameter.
Each time you call one of the randomization functions, the starting point stored for the random number generator is updated even if no numbers are returned. This means that if you call the function repeatedly without specifying the <seed> parameter, the sequence of results is determined by the changing value passed as a hidden parameter. Specifying the <seed> parameter resets this sequence and means that the sequence of returned values can be repeated. For example, if you use this function to assign random responses to a categorical question, by using the same <seed> value you could reassign the same responses.
Examples
You can use RanSequence to generate random responses to a categorical question. To illustrate this, consider the
Remember and
Interest variables in the Museum sample UNICOM Intelligence Data File. These are categorical variables that have identical category lists that contain categories for the various museum galleries. The categories that represent the various galleries have
mapped category values of 31 through 45. The following query uses the
IIf function to test whether the value of the
Interest variable is Null. If the value is Null, RanSequence is used to assign a single response chosen at random from the possible response values. The multiple response
Remember variable is handled in a similar way, except three responses are assigned at random.
SELECT Respondent.Serial,
IIf(Interest IS NULL, RanSequence(31, 45, 1, 1), Interest) AS Interest,
IIf(Remember IS NULL, RanSequence(31, 45, 1, 3), Remember) AS Remember
FROM vdata
Here are the results for the first five respondents, for whom the Interest and Remember variables are Null in the case data:
Respondent.Serial Interest Remember
----------------- -------------- ----------------------------------------
1 {insects} {insects,minerals,origin_of_species}
2 {ecology} {insects,dinosaurs,mammals}
3 {conservation} {dinosaurs,ecology,whales}
4 {insects} {fish_and_reptiles,botany,human_biology}
5 {conservation} {conservation,evolution,whales}
The following example uses RanSequence and
CLong to assign to a variable a number taken randomly from a series of numbers. The series of numbers from which the random number is chosen is 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.
Dim GroupID
GroupID = CLong(RanSequence(0, 100, 10, 1))
This example illustrates using RanSequence in mrScriptBasic. It might not be something you would do in practice; there are other ways you could achieve the same result. For example:
GroupID = 10 * CLong(11 * Rnd())
GroupID = 10 * (RanInt() Mod 11)
See also