UpdateSeed (RandomNumberGenerator subroutine): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 12: Line 12:
<tr><th>Seed=seed</th>
<tr><th>Seed=seed</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.
<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.
''seed'' is a string or a longstring. </td></tr>
<p class="code">''seed'' is a string or a longstring. </td></tr>
</p>
<tr><th>Salt=salt</th>
<tr><th>Salt=salt</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.
''salt'' is a string or a longstring.</td></tr>
<p class="code">''salt'' is a string or a longstring.</td></tr>
</p>
</table>
</table>
==Examples==
==Examples==
Line 22: Line 24:
series of three values of %rand that matches the initial series of three
series of three values of %rand that matches the initial series of three
values of %rand:
values of %rand:
<pre>
<p class="code">begin
    begin
%i is float
    %i is float
%rand is object RandomNumberGenerator
    %rand is object RandomNumberGenerator


    %rand = new(seed='help')
%rand = new(seed='help')
    for %i from 1 to 3
for %i from 1 to 3
      printtext {~} = {%rand:value}
  printtext {~} = {%rand:value}
    end for
end for


    %rand:updateSeed(seed='help')
%rand:updateSeed(seed='help')
    for %i from 1 to 3
for %i from 1 to 3
      printtext {~} = {%rand:value}
  printtext {~} = {%rand:value}
    end for
end for
    end
end
</pre>
</p>


The result is:
The result is:
<pre>
<p class="output">%rand:value = 816969049
    %rand:value = 816969049
%rand:value = -148817379
    %rand:value = -148817379
%rand:value = 895846046
    %rand:value = 895846046
%rand:value = 816969049
    %rand:value = 816969049
%rand:value = -148817379
    %rand:value = -148817379
%rand:value = 895846046
    %rand:value = 895846046
</p>
</pre>


If instead the random number is created with a salt,
If instead the random number is created with a salt,
Line 53: Line 53:
an updateSeed call using the salt produces a unique series of
an updateSeed call using the salt produces a unique series of
three values (salted numbers are not reproducible):
three values (salted numbers are not reproducible):
<pre>
<p class="code">...
    ...
%rand = new(salt='iou')
    %rand = new(salt='iou')
for %i from 1 to 3
    for %i from 1 to 3
  printtext {~} = {%rand:value}
      printtext {~} = {%rand:value}
end for
    end for


    %rand:updateSeed(salt='iou')
%rand:updateSeed(salt='iou')
    for %i from 1 to 3
for %i from 1 to 3
      printtext {~} = {%rand:value}
  printtext {~} = {%rand:value}
    end for
end for
</pre>
</p>


A sample result is:
A sample result is:
<pre>
<p class="output">%rand:value = 320947512
    %rand:value = 320947512
%rand:value = 535019615
    %rand:value = 535019615
%rand:value = -526589924
    %rand:value = -526589924
%rand:value = 387949238
    %rand:value = 387949238
%rand:value = 413468100
    %rand:value = 413468100
%rand:value = -807607538
    %rand:value = -807607538
</p>
</pre>
==See also==
==See also==
{{Template:RandomNumberGenerator:UpdateSeed footer}}
{{Template:RandomNumberGenerator:UpdateSeed footer}}

Revision as of 21:48, 6 February 2011

Reset random sequence with new salt and seed (RandomNumberGenerator class)


This method sets a random number object to the number that would be created by the New constructor with the given Seed and Salt specifications.

Syntax

randomNumberGenerator:UpdateSeed[( [Seed= string], [Salt= string])]

Syntax terms

%rand A RandomNumberGenerator object variable.
Seed=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.

seed is a string or a longstring.

Salt=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 or a longstring.

Examples

In the following request, calling updateSeed enables the production of a second series of three values of %rand that matches the initial series of three values of %rand:

begin %i is float %rand is object RandomNumberGenerator %rand = new(seed='help') for %i from 1 to 3 printtext {~} = {%rand:value} end for %rand:updateSeed(seed='help') for %i from 1 to 3 printtext {~} = {%rand:value} end for end

The result is:

%rand:value = 816969049 %rand:value = -148817379 %rand:value = 895846046 %rand:value = 816969049 %rand:value = -148817379 %rand:value = 895846046

If instead the random number is created with a salt, as in the following fragment, an updateSeed call using the salt produces a unique series of three values (salted numbers are not reproducible):

... %rand = new(salt='iou') for %i from 1 to 3 printtext {~} = {%rand:value} end for %rand:updateSeed(salt='iou') for %i from 1 to 3 printtext {~} = {%rand:value} end for

A sample result is:

%rand:value = 320947512 %rand:value = 535019615 %rand:value = -526589924 %rand:value = 387949238 %rand:value = 413468100 %rand:value = -807607538

See also