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 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 is 0.
(return)
Type: None
An array containing a random selection from the set of possible numbers.
Remarks
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 may 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 > End, or if Step is negative and Start < 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 will use 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 is a way of resetting 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, let's 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 next example is a snippet of mrScriptBasic that 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 is provided to illustrate using RanSequence in mrScriptBasic and is not necessarily something you will want to do in practice and there are in fact other ways you could achieve the same result. For example:
GroupID = 10 * CLong(11 * Rnd())
GroupID = 10 * (RanInt() Mod 11)
See also