Difference between revisions of "RandomSeed"
m |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
[[category:System Variables]] | [[category:System Variables]] | ||
+ | |||
+ | RandomSeed is a system variable used in generating random numbers for samples from probability distributions. You can reset RandomSeed if you want to reproduce the same set of random numbers for comparison purposes. | ||
== System Variable RandomSeed == | == System Variable RandomSeed == | ||
− | The random samples and random numbers generated by Analytica, or | + | The random numbers and samples and random numbers generated by Analytica, or any software, are not truly [[random]]. They are ''pseudo-random sequences'' generated by a deterministic algorithm. The "random seed" captures the state of the pseudo-random number algorithm. When an algorithm starts from the same initial seed, it produces the same random sequence. You can reset the [[RandomSeed]] in the [[Uncertainty Setup Dialog]], or directly by assigning an integer to the system variable [[RandomSeed]]. |
− | + | The value of [[RandomSeed]] does not change as it generates random numbers are generated, even though the internal seed does change. The internal seed has many more bits than fit in a single number, and is not accessible. Instead, [[RandomSeed]] stays at the previous value that you set it to. If you save the model after change [[RandomSeed]], the random number generator will start from that new seed when your model is reloaded. | |
− | + | You are allowed to assign to [[RandomSeed]] using the [[Assignment Operator :=]] even inside a variable [[Definition]], which of course is in direct violation to the "[[Assignment Operator :=|no side-effects rule]]". (But, you should not change it using [[EvaluateScript]] while a variable is evaluating). When you assign a new value to [[RandomSeed]], it does '''not''' invalidate existing random numbers or samples. Assigning to [[RandomSeed]] is useful in the Definiton of a Chance variable when you want to ensure that the sample us the same, independent of the order in which it evaluates chance variables, or whether the variable is re-evaluated multiple times. | |
− | |||
− | You are allowed to assign to [[RandomSeed]] using the [[Assignment Operator :=]] even | ||
== Example == | == Example == | ||
Line 19: | Line 19: | ||
If you have a second chance variable that is also [[Normal]](0,1) distributed, you would use a different [[RandomSeed]] number so that its sample is not identical to the first variable. | If you have a second chance variable that is also [[Normal]](0,1) distributed, you would use a different [[RandomSeed]] number so that its sample is not identical to the first variable. | ||
− | == | + | == Assigning in EvaluateScript or Typescript == |
− | + | ||
− | Changing [[RandomSeed]] using an assignment operator is not the same as setting it from | + | Changing [[RandomSeed]] using an assignment operator in a standard Definition is not the same as setting it from [[Typescript]] or inside [[EvaluateScript]]. When you assign to [[RandomSeed]] in a Definition, it does not invalidate previously evaluated random numbers. But when you assign to [[RandomSeed]] in a [[Typescript]] or inside the parameter to [[EvaluateScript]], it ''does'' invalidate all previously cached uncertain variables. |
:<code>RandomSeed := 5; [[Uniform]](0,1)</code> { Generates a repeatable sequence } | :<code>RandomSeed := 5; [[Uniform]](0,1)</code> { Generates a repeatable sequence } | ||
:<code>EvaluateScript("RandomSeed:5")</code> { Invalidates all uncertain variables in the model } | :<code>EvaluateScript("RandomSeed:5")</code> { Invalidates all uncertain variables in the model } | ||
− | You should never set it via [[EvaluateScript]] while a variable evaluation is in-progress. In general, [[EvaluateScript]] | + | You should never set it via [[EvaluateScript]] while a variable evaluation is in-progress. In general, you should avoid using [[EvaluateScript]] expressions when a variable evaluation is in-progress, since it does let you do things that may be dangerous. You may sometimes use it as a kind of "hack" -- for example, to produce a side-effect that you know is safe despite the no-side-effect rule. Usually that means the side-effect does not impact any upstream variables of the current variable, and is not upstream from any computed indexes that might appear in tables, choices, etc. [[RandomSeed]] is almost guaranteed to be upstream, and so is almost always dangerous to set via [[EvaluateScript]]. |
== See Also == | == See Also == | ||
Line 33: | Line 33: | ||
* [[SampleType]] | * [[SampleType]] | ||
* [[RandomType]] | * [[RandomType]] | ||
+ | * [[Assignment Operator :=]] |
Latest revision as of 19:24, 16 October 2018
RandomSeed is a system variable used in generating random numbers for samples from probability distributions. You can reset RandomSeed if you want to reproduce the same set of random numbers for comparison purposes.
System Variable RandomSeed
The random numbers and samples and random numbers generated by Analytica, or any software, are not truly random. They are pseudo-random sequences generated by a deterministic algorithm. The "random seed" captures the state of the pseudo-random number algorithm. When an algorithm starts from the same initial seed, it produces the same random sequence. You can reset the RandomSeed in the Uncertainty Setup Dialog, or directly by assigning an integer to the system variable RandomSeed.
The value of RandomSeed does not change as it generates random numbers are generated, even though the internal seed does change. The internal seed has many more bits than fit in a single number, and is not accessible. Instead, RandomSeed stays at the previous value that you set it to. If you save the model after change RandomSeed, the random number generator will start from that new seed when your model is reloaded.
You are allowed to assign to RandomSeed using the Assignment Operator := even inside a variable Definition, which of course is in direct violation to the "no side-effects rule". (But, you should not change it using EvaluateScript while a variable is evaluating). When you assign a new value to RandomSeed, it does not invalidate existing random numbers or samples. Assigning to RandomSeed is useful in the Definiton of a Chance variable when you want to ensure that the sample us the same, independent of the order in which it evaluates chance variables, or whether the variable is re-evaluated multiple times.
Example
The Definition of a Chance variable might be:
RandomSeed := 9;
Normal(0,1)
If you have a second chance variable that is also Normal(0,1) distributed, you would use a different RandomSeed number so that its sample is not identical to the first variable.
Assigning in EvaluateScript or Typescript
Changing RandomSeed using an assignment operator in a standard Definition is not the same as setting it from Typescript or inside EvaluateScript. When you assign to RandomSeed in a Definition, it does not invalidate previously evaluated random numbers. But when you assign to RandomSeed in a Typescript or inside the parameter to EvaluateScript, it does invalidate all previously cached uncertain variables.
RandomSeed := 5; Uniform(0,1)
{ Generates a repeatable sequence }EvaluateScript("RandomSeed:5")
{ Invalidates all uncertain variables in the model }
You should never set it via EvaluateScript while a variable evaluation is in-progress. In general, you should avoid using EvaluateScript expressions when a variable evaluation is in-progress, since it does let you do things that may be dangerous. You may sometimes use it as a kind of "hack" -- for example, to produce a side-effect that you know is safe despite the no-side-effect rule. Usually that means the side-effect does not impact any upstream variables of the current variable, and is not upstream from any computed indexes that might appear in tables, choices, etc. RandomSeed is almost guaranteed to be upstream, and so is almost always dangerous to set via EvaluateScript.
Enable comment auto-refresher