Language Reference
RANDSEED Call
CALL RANDSEED (seed <, reinit> ) ;
CALL RANDSEED (RNG <, seed> <, reinit> ) ;
This subroutine is supported by the IML procedure and the iml action.
The RANDSEED subroutine sets the initial state for the RANDGEN subroutine and the RANDFUN function. It also sets the seed for routines that generate random combinations (RANCOMB), permutations (RANPERK and RANPERM), and samples (SAMPLE).
- RNG
is a string that specifies a valid random-number generator (RNG).
- seed
is a number to be used to initialize the RANDGEN random number generator.
- reinit
specifies whether the random number stream should be reinitialized after the first initialization, within the same PROC IML session.
Table 7 shows valid values for the RNG argument. These random-number generators are described in the documentation for the STREAMINIT call in SAS Functions and CALL Routines: Reference.
Table 7: Random-Number Generators That Are Supported in SAS
| Value | Description |
|---|---|
| MTHYBRID | (Default) Hybrid 1998/2002 32-bit Mersenne twister |
| MT1998 | (Deprecated) 1998 32-bit Mersenne twister |
| MT32 | MT2002 | 2002 32-bit Mersenne twister |
| MT64 | 64-bit Mersenne twister |
| PCG | PCG64I | 64-bit permuted congruential generator |
| TF2 | THREEFRY2X64 | Threefry 2x64-bit counter-based RNG |
| TF4 | THREEFRY4X64 | Threefry 4x64-bit counter-based RNG |
| HARDWARE | Any supported hardware-based random number generator |
| RDRAND | Intel hardware-based RdRand instructions |
The RANDSEED subroutine enables you to specify an initial seed for subsequent RANDGEN calls. A seed is automatically generated if the RANDSEED subroutine is not called, if you specify 0 as a seed, or if you use a calling syntax that does not specify a seed. The initial seed is obtained from the Intel RdRand instruction on CPUs that support that instruction, or from the current datetime value otherwise.
Hardware-based RNGs do not use seeds. If you specify a seed for a hardware-based RNG, the seed is silently ignored. If you request a hardware-based RNG but it is not available on your CPU, then the default RNG is used.
The RANDSEED routine is used to generate a reproducible stream of pseudorandom numbers. The optional reinit parameter controls whether the random number stream is reinitialized after it has begun generating random numbers. If the second argument is 1, the stream is reinitialized; otherwise, subsequent calls to the RANDSEED subroutine are ignored. To ensure that you are working with an independent random number stream within your PROC IML session, set reinit to 0 or do not specify that argument.
In the third maintenance release of SAS 9.4 software, the MTHYBRID generator became the default RNG, instead of the earlier default, the MT1998 generator. The MTHYBRID generator is a hybrid between the MT1998 and the MT2002 generators. When the seed is exactly divisible by 8192, the MTHYBRID algorithm uses the 2002 initialization algorithm (Matsumoto and Nishimura 2002). Otherwise, the algorithm is initialized by using the 1998 initialization algorithm (Matsumoto and Nishimura 1998). A SAS Note appears when the 2002 initialization is used.
proc iml;
call randseed(12345); /* use default RNG; set seed */
x1 = randfun(5, "uniform"); /* get 5 random uniform variates */
call randseed(12345, 1); /* reset current stream, same seed */
x2 = randfun(5, "uniform"); /* x2 equals x1 */
call randseed(4321, 1); /* reset current stream, different seed */
x3 = randfun(5, "uniform"); /* x3 is different from x1 */
/* switch to new RNG */
call randseed("PCG", 9876, 1); /* use PCG RNG */
y1 = randfun(5, "uniform"); /* get 5 random uniform variates */
call randseed("PCG", 54321, 1); /* reset PCG with new seed */
y2 = randfun(5, "uniform"); /* y2 is different from y1 */
call randseed("TF2", 54321, 1); /* use TF2 RNG; same seed */
y3 = randfun(5, "uniform"); /* y3 is different from y2 */
r = x1 || x2 || x3 || y1 || y2 || y3;
print r[F=5.3 C={x1 x2 x3 y1 y2 y3} L="Random Uniform Variates"];
quit;
Figure 350: Random Uniform Variates from Different RNGs and Seeds
| Random Uniform Variates | |||||
|---|---|---|---|---|---|
| X1 | X2 | X3 | Y1 | Y2 | Y3 |
| 0.583 | 0.583 | 0.791 | 0.407 | 0.167 | 0.088 |
| 0.994 | 0.994 | 0.375 | 0.599 | 0.864 | 0.391 |
| 0.588 | 0.588 | 0.930 | 0.916 | 0.135 | 0.714 |
| 0.857 | 0.857 | 0.209 | 0.019 | 0.886 | 0.726 |
| 0.825 | 0.825 | 0.216 | 0.337 | 0.374 | 0.479 |