Distinguishing Random Number Seeding from Random Number Generation
Summary
The document clarifies a basic distinction that matters when reproducing Monte Carlo simulations: in MATLAB, rng configures the state or seed of the random number generator, while rand produces random values. Calling a seeding function is not equivalent to drawing a sample, so substituting one for the other changes what the program does and can alter the sequence of values used by a simulation.
The question arose after different calls appeared to produce different behavior in an option-pricing utility. The reply identifies the function mix-up and notes that rng accepts positive integer seeds. It does not diagnose the reported negative-option-value error or explain the option routine's inputs, so the exchange alone cannot establish why the error occurred. The useful lesson is to seed the generator explicitly when reproducibility is needed, then draw values using the appropriate sampling function and check the simulation's assumptions separately.
Key ideas
- The rng function sets the random number generator state or seed.
- The rand function generates random values from the generator.
- Use an appropriate seed to make simulation sequences reproducible.
- The exchange does not establish the cause of the reported option-value error.
Tags
Full text
# Matlab - Differences between rng and rand # Matlab - Differences between rng and rand I was trying to run some Monte-Carlo simulations and if I used: rng(seed, 'Twister'); For some reason I would get "Option Values Can not be Negative" errors in the blsimpv function, but if I just use the rand (seed) function, the problem seems to be OK (having everything else the same). My question is, aren't both of them the similar function? (i.e., generate a whole bunch of random numbers). If so, why is one giving me issues and the other is OK? I post this is Quant since this is related to the implementation of quant models, hope that is OK. Thanks in advance! ## Answer by Ander Biguri (score 3, accepted) https://quant.stackexchange.com/a/7577 No, `rng` does not do the same as `rand`. `rng` sets the seed for the random number generator and `rand` generates random numbers. Also it can be seen in documentation that the `rng` function only accepts positive integers. Usually random number generator algorithms start with integers for the seed. For various examples: C random function takes the system clock as seed as it does Minecraft's world generator. Doc: rnd rand
Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.