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 by any other software, are not truly [[random]]. They are called pseudo-random sequences. They are generated by deterministic algorithms. When these algorithms are started from the same initial state, they will reproduce the same random sequence. The "random seed" captures the state of the pseudo-random number algorithm.
+
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]].  
  
This system variable allows you to reset the [[RandomSeed]] to an integer value.  
+
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.
  
The value of the system variable does not change as 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 the model is saved after you change this, then the random number generator will start from that new seed after your model is loaded.
+
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 from variable [[Definition]]s, 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). Previously generated random numbers are NOT invalidated from this assignment. Doing this is quite useful when you want to ensure that your sequence always comes out the same, independent of the order in which the user evaluates variables, and independent of the number of times the variable has been re-evaluated.
 
  
 
== 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.
  
== Notes ==
+
== Assigning in EvaluateScript or Typescript ==
=== Assignment vs. EvaluateScript ==
+
 
Changing [[RandomSeed]] using an assignment operator is not the same as setting it from typescript. When set from an expression using the assignment operator, previously evaluated random numbers are ''not'' invalidated. But when set from typescript or by using [[EvaluateScript]], all previously cached uncertain variables are invalidated.
+
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]] should always be avoided from expressions when a variable evaluation is in-progress, since it does let you do things that are not legitimate. It is, however, sometimes used as a hack, for example, to enact a side-effect that you know is safe despite the no-side-effect rule (which would generally mean that 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 me upstream, and hence is almost always dangerous to set via [[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.

See Also

Comments


You are not allowed to post comments.