New (RandomNumberGenerator constructor): Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
<li>If no parameters are specified, <var>New</var> returns an unpredictable random number | <li>If no parameters are specified, <var>New</var> returns an unpredictable random number | ||
each time it is called. | each time it is called. | ||
<li>If a Salt parameter is specified, <var>New</var> returns an unpredictable random number | |||
<li>If a <var>Salt</var> parameter is specified, <var>New</var> returns an unpredictable random number | |||
that is based on initial input that has an extra degree of randomness. | that is based on initial input that has an extra degree of randomness. | ||
<li>If only a Seed parameter is specified, <var>New</var> returns the next number in a | |||
<li>If only a <var>Seed</var> parameter is specified, <var>New</var> returns the next number in a | |||
specific series of pseudo-random numbers. | specific series of pseudo-random numbers. | ||
Subsequent identical calls produce the next items in the series; | Subsequent identical calls produce the next items in the series; | ||
recreating or resetting the object with the same seed lets you reproduce the series. | recreating or resetting the object with the same seed lets you reproduce the series. | ||
</ul> | </ul> | ||
==Syntax== | ==Syntax== | ||
{{Template:RandomNumberGenerator:New syntax}} | {{Template:RandomNumberGenerator:New syntax}} | ||
===Syntax terms=== | ===Syntax terms=== | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>%randomNumberGenerator</th> | <tr><th>%randomNumberGenerator</th> | ||
<td>A <var>RandomNumberGenerator</var> object variable. </td></tr> | <td>A <var>RandomNumberGenerator</var> object variable. </td></tr> | ||
<tr><th><var>%(RandomNumberGenerator)</var></th> | |||
<tr><th><var>[%(RandomNumberGenerator):]</var></th> | |||
<td>The optional class name in parentheses denotes a <var>[[Notation conventions for methods#Constructors|Constructor]]</var>. See [[#Usage notes|"Usage notes"]], below, for more information about invoking a <var>RandomNumberGenerator</var> <var>Constructor</var>.</td></tr> | <td>The optional class name in parentheses denotes a <var>[[Notation conventions for methods#Constructors|Constructor]]</var>. See [[#Usage notes|"Usage notes"]], below, for more information about invoking a <var>RandomNumberGenerator</var> <var>Constructor</var>.</td></tr> | ||
<tr><th><var>Seed</var></th> | <tr><th><var>Seed</var></th> | ||
<td>An optional, name required, argument that lets you specify a "seed" for the random number generator. If you provide no seed, the method generates a new, "random," starting value for the random number generator, which produces an unpredictable random number result. | <td>An optional, [[Notation conventions for methods#Named parameters|name required]], argument that lets you specify a "seed" for the random number generator. If you provide no seed, the method generates a new, "random," starting value for the random number generator, which produces an unpredictable random number result. | ||
<var>Seed</var> is a string. </td></tr> | <var>Seed</var> is a string. </td></tr> | ||
<tr><th><var>Salt</var></th> | <tr><th><var>Salt</var></th> | ||
<td>An optional, name required, argument that lets you specify a "salt" for the random number generator. A salt is additional data to be used (along with the explicit or default seed) to improve the randomness of the random generator's initial value. | <td>An optional, name required, argument that lets you specify a "salt" for the random number generator. A salt is additional data to be used (along with the explicit or default seed) to improve the randomness of the random generator's initial value. | ||
<var>Salt</var> is a string.</td></tr> | <var>Salt</var> is a string.</td></tr> | ||
</table> | </table> | ||
==Usage notes== | ==Usage notes== | ||
<ul> | <ul> | ||
<li>As described in [[Object variables#Using New or other Constructors|"Using New or other Constructors"]], <var>New</var> can be invoked with no object, with an explicit class name, or with an object variable in the class, even if that object is <var>Null</var>:<p class="code">%randomNumberGenerator= new | <li>As described in [[Object variables#Using New or other Constructors|"Using New or other Constructors"]], <var>New</var> can be invoked with no object, with an explicit class name, or with an object variable in the class, even if that object is <var>Null</var>: | ||
<p class="code">%randomNumberGenerator= new | |||
%randomNumberGenerator= %(RandomNumberGenerator):new | %randomNumberGenerator= %(RandomNumberGenerator):new | ||
Line 40: | Line 47: | ||
%randomNumberGenerator= %randomNumberGenerator:new | %randomNumberGenerator= %randomNumberGenerator:new | ||
</p> | </p> | ||
<li>If no Seed argument is specified, the <var>New</var> method generates a random seed | |||
<li>If no <var>Seed</var> argument is specified, the <var>New</var> method generates a random seed | |||
(based on multiple, internal system data like STCK value, frequently-changed parts of | (based on multiple, internal system data like STCK value, frequently-changed parts of | ||
memory, and so on). | memory, and so on). | ||
<li>If you want to increase the randomness of the initial value passed to the random | <li>If you want to increase the randomness of the initial value passed to the random | ||
number generator, but you don't want a reproducible sequence, you can pass | number generator, but you don't want a reproducible sequence, you can pass | ||
additional data to be used to | additional data to be used to | ||
generate the random starting value, maybe from something a user types in. | generate the random starting value, maybe from something a user types in. | ||
This additional value is called a < | This additional value is called a <b>salt</b>. | ||
<li>Since the likely use of explicitly specifying a seed | <li>Since the likely use of explicitly specifying a seed | ||
is to produce a reproducible random sequence, there is probably not much reason | is to produce a reproducible random sequence, there is probably not much reason | ||
to specify both a salt and a seed, which would nullify reproducibility. | to specify both a salt and a seed, which would nullify reproducibility. | ||
<li> | |||
<li>The <var>RandomNumberGenerator</var> class page has an [[RandomNumberGenerator class#Example|example]] of the <var>New</var> method. | |||
</ul> | </ul> | ||
==See also== | ==See also== | ||
{{Template:RandomNumberGenerator:New footer}} | {{Template:RandomNumberGenerator:New footer}} |
Latest revision as of 21:29, 6 November 2012
Create a new RandomNumberGenerator (RandomNumberGenerator class)
The New method creates a random number.
The kind of random number depends on the parameters specified when New is invoked:
- If no parameters are specified, New returns an unpredictable random number each time it is called.
- If a Salt parameter is specified, New returns an unpredictable random number that is based on initial input that has an extra degree of randomness.
- If only a Seed parameter is specified, New returns the next number in a specific series of pseudo-random numbers. Subsequent identical calls produce the next items in the series; recreating or resetting the object with the same seed lets you reproduce the series.
Syntax
%randomNumberGenerator = [%(RandomNumberGenerator):]New[( [Seed= string], - [Salt= string])]
Syntax terms
%randomNumberGenerator | A RandomNumberGenerator object variable. |
---|---|
[%(RandomNumberGenerator):] | The optional class name in parentheses denotes a Constructor. See "Usage notes", below, for more information about invoking a RandomNumberGenerator Constructor. |
Seed | An optional, name required, argument that lets you specify a "seed" for the random number generator. If you provide no seed, the method generates a new, "random," starting value for the random number generator, which produces an unpredictable random number result. Seed is a string. |
Salt | An optional, name required, argument that lets you specify a "salt" for the random number generator. A salt is additional data to be used (along with the explicit or default seed) to improve the randomness of the random generator's initial value. Salt is a string. |
Usage notes
- As described in "Using New or other Constructors", New can be invoked with no object, with an explicit class name, or with an object variable in the class, even if that object is Null:
%randomNumberGenerator= new %randomNumberGenerator= %(RandomNumberGenerator):new %randomNumberGenerator= %randomNumberGenerator:new
- If no Seed argument is specified, the New method generates a random seed (based on multiple, internal system data like STCK value, frequently-changed parts of memory, and so on).
- If you want to increase the randomness of the initial value passed to the random number generator, but you don't want a reproducible sequence, you can pass additional data to be used to generate the random starting value, maybe from something a user types in. This additional value is called a salt.
- Since the likely use of explicitly specifying a seed is to produce a reproducible random sequence, there is probably not much reason to specify both a salt and a seed, which would nullify reproducibility.
- The RandomNumberGenerator class page has an example of the New method.