Hello, I'm Rick Wicklin from SAS.
Today, I'm talking about how to use
the new random-number generators that
are supported in SAS 9.4 M5.
My co-author on this paper is Warren Sarle.
I want to communicate four main ideas.
There are six new random-number generators in SAS 9.4 M5.
You can generate multiple streams
from a random-number generator.
You can use multiple streams to generate random numbers
in parallel.
And you can use the properties of the random-number generators
to determine which is most suitable for your applications.
A random-number generator produces uniformly distributed
numbers in the interval 0 to 1.
The infinite sequence of numbers is what we call a stream.
Now there are two types of random-number generators.
The first is called a pseudorandom-number generator.
The generator is actually a mathematical function
that is initialized by a so-called seed value.
The seed determines an internal state,
and the function uses its internal state
to generate a random number.
It then updates its internal state
so that the next time you ask for a random number
it gives you a different one.
The second type of random-number generator
is a hardware-based method.
It uses a source of entropy on a computer chip
to provide the seed value.
It then initializes and produces a short sequence
from a pseudorandom-number generator
before getting the new seed value from the chip
and reseeding the algorithm.
As mentioned earlier, SAS 9.4 M5 provides
six new generators in addition to the existing Mersenne
twister generator, which is the default.
Two of the new generators are variations
of the Mersenne twister.
One is called the permuted congruential generator or PCG
for short.
Two are counter-based random-number generators
that are based on the threefry encryption function.
For short, they are called TF2 and TF4.
The last random-number generator is a hardware-based method
which uses and is named after the RDRAND instruction on Intel
chips.
The uppercase names on this slide
indicates the string that you need
to specify in order to get each random-number generator in SAS.
There is one small optional change to the SAS syntax
that generates random numbers.
The STREAMINIT call, which formerly took only a seed
value, now accepts an optional name of a random number
generator such as PCG or TF2.
The call to the RAND function which actually generates
the random numbers from a probability distribution
is unchanged.
It continues to take the name of a probability distribution
and the parameters for that distribution.
The two data steps on this slide indicate
how to use the STREAMINIT call to specify
a random-number generator.
Both data steps use the RAND function to generate
a row of five random values.
In the first data step, the PCG argument and a seed value
are specified to the STREAMINIT call.
Previously only a seed value was accepted.
If you do not specify the name of a generator,
the default generator is used, which
is the hybrid Mersenne twister.
In the second data step, the RDRAND argument is specified.
Recall that the RDRAND generator gets its seed value
from an entropy source on a computer chip,
so you do not need to specify a seed value in this case.
As mentioned earlier, SAS 9.4 M5 supports multiple streams
from the same seed value.
Several of the new random number generators
support a second seed called a key.
For the PCG and the threefry generators,
two streams that use the same seed but different keys
are statistically independent.
For most applications of random-number generation,
you will not need to use keys or more than one string.
Streams are important in two situations.
The first is when you are writing
a macro that takes a seed value and calls two or more data
steps, each of which generates random values.
The other situation is when you generate random numbers
concurrently in multiple threads.
This slide shows an example of a SAS macro
that calls two data steps, each of which
generates random numbers.
The macro uses a seed value to initialize each data step.
Now without further intervention,
the numbers generated in the second data step
are not independent from the numbers in the first data step.
Now this potential problem is resolved by calling the STREAM
function in each data step.
The first data step calls the STREAM function
with a key value of 1.
The second uses a key value of 2.
For the PCG and threefry generators,
the random-number stream in the second data step
is independent from that in the first data step.
A second situation in which streams are useful
is when you use the DS2 procedure to generate
random numbers in parallel.
On this slide, the four statements in the init method
specify what program each thread will run.
Now you can see that each thread will
set the random number generator to TF2 and set a seed value.
Then each thread generates 2,500 random normal variates.
Lower down in the program on the slide is the run() method.
In the run() method, the set statement will run the threaded
program in four threads and put all of the results into a data
set named C. The data set will contain a total of 10,000
random numbers.
Now although this program does not explicitly
use the STREAM call, SAS calls STREAM automatically
for threaded programs.
The key value is derived from the ID for each thread.
Thus the first thread is assigned
a key value that is related to the thread ID of 1.
The second thread is given a key value
that is related to the thread ID of 2, and so forth.
Therefore the four threads generate independent streams
of random numbers.
With so many random-number generators available,
how can you choose the random-number generator that's
right for your application?
This table is explained in detail in my SAS Global Forum
2018 paper.
Briefly, it indicates the quality
of each random-number generator with respect
to a rigorous series of tests that
assess whether the random-number streams appear
to be statistically random.
All the new random-number generators pass these tests.
The last column in the table indicates how long
it takes each generator to generate
a billion random numbers.
The number in the column is a multiplier
which is based on the length of time
that the Mersenne twister method requires to generate
a billion random numbers.
The table says that, for example, the PCG method takes
about 1.2 times that long.
The TF2 method takes about 1.5 times that long, and so forth.
If you are happy with the random numbers
that SAS has provided in the past,
you're welcome to keep using the fast Mersenne twister
algorithm.
However, if you have need to generate random numbers
in parallel or to ensure that multiple streams are
independent, then I suggest you switch to the new PCG algorithm
which is fast and very random.
Thank you for watching.
I hope you've enjoyed learning a little bit
about the new random-number generators in SAS 9.4 M5.
More information about this topic
is available in my 2018 SAS Global Forum
paper, and the book Simulating Data with SAS
contains many applications of using
random numbers in statistics.
I also write a blog that often demonstrates
simulation techniques for statistical programmers.