Random things to say to your friends When someone says “have a nice day”, stare at them and say, “don’t tell me what to do”! Make a cardboard car and go through a local drive through, then act as if everything’s normal. Pretend to pass out in a busy place. Just set the range to be between 1 and 100, and use the output to determine which action you should make depending upon your pre-determined frequencies. For instance, say you want to 3-bet your AJo combos 33% of the time and call 67% in a certain scenario.
Create Arrays of Random Numbers
MATLAB® uses algorithms to generate pseudorandom and pseudoindependent numbers. These numbers are not strictly random and independent in the mathematical sense, but they pass various statistical tests of randomness and independence, and their calculation can be repeated for testing or diagnostic purposes.
The rand
, randi
, randn
, and randperm
functions are the primary functions for creating arrays of random numbers. The rng
function allows you to control the seed and algorithm that generates random numbers.
Random Number Functions
There are four fundamental random number functions: rand
, randi
, randn
, and randperm
. The rand
function returns real numbers between 0 and 1 that are drawn from a uniform distribution. For example:
r1
is a 1000-by-1 column vector containing real floating-point numbers drawn from a uniform distribution. All the values in r1
are in the open interval (0, 1). A histogram of these values is roughly flat, which indicates a fairly uniform sampling of numbers.The randi
function returns double
integer values drawn from a discrete uniform distribution. For example,
r2
is a 1000-by-1 column vector containing integer values drawn from a discrete uniform distribution whose range is in the close interval [1, 10]. A histogram of these values is roughly flat, which indicates a fairly uniform sampling of integers between 1 and 10. The randn
function returns arrays of real floating-point numbers that are drawn from a standard normal distribution. For example:
r3
is a 1000-by-1 column vector containing numbers drawn from a standard normal distribution. A histogram of r3
looks like a roughly normal distribution whose mean is 0 and standard deviation is 1.You can use the randperm
function to create a double
array of random integer values that have no repeated values. For example,
r4
is a 1-by-5 array containing integers randomly selected from the range [1, 15]. Unlike Random Bets To Make Friends
randi
, which can return an array containing repeated values, the array returned by randperm
has no repeated values.Successive calls to any of these functions return different results. This behavior is useful for creating several different arrays of random values.
Random Number Generators
MATLAB offers several generator algorithm options, which are summarized in the table.
Value | Generator Name | Generator Keyword |
---|---|---|
'twister' | Mersenne Twister (used by default stream at MATLAB startup) | mt19937ar |
'simdTwister' | SIMD-oriented Fast Mersenne Twister | dsfmt19937 |
'combRecursive' | Combined multiple recursive | mrg32k3a |
'multFibonacci' | Multiplicative Lagged Fibonacci | mlfg6331_64 |
'philox' | Philox 4x32 generator with 10 rounds | philox4x32_10 |
'threefry' | Threefry 4x64 generator with 20 rounds | threefry4x64_20 |
'v4' | Legacy MATLAB version 4.0 generator | mcg16807 |
'v5uniform' | Legacy MATLAB version 5.0 uniform generator | swb2712 |
'v5normal' | Legacy MATLAB version 5.0 normal generator | shr3cong |
Use the rng
function to set the seed and generator used by the rand
, randi
, randn
, and randperm
functions. For example, rng(0,'twister')
reset the generator to its default state. To avoid repetition of random number arrays when MATLAB restarts, see Why Do Random Numbers Repeat After Startup?
For more information about controlling the random number generator's state to repeat calculations using the same random numbers, or to guarantee that different random numbers are used in repeated calculations, see Controlling Random Number Generation.
Random Number Data Types
rand
and randn
functions generate values in double precision by default.
To specify the class as double explicitly:
rand
and randn
can also generate values in single precision.
The values are the same as if you had cast the double precision values from the previous example. The random stream that the functions draw from advances the same way regardless of what class of values is returned.
randi
supports both integer types and single or double precision.
See Also
rand
| randi
| randn
| randperm
| rng