Subtractive generator: Difference between revisions

Added PowerShell
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
(Added PowerShell)
Line 1,233:
80746560
563900213
</pre>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
The so-called modulus operator in PowerShell (%) returns a remainder not a modulus. Hence the need for the custom Mod function when working with negative numbers.
( X % M + M ) % M can be replaced with ( X + M ) % M when X is always between -M and M, as is the case in this task, but the former is used for clarity.
The first 55 generated values are placed directly into their reordered slots in the ring.
An array object is used along with a rotating index object to simulate a ring.
<lang PowerShell>
function Get-SubtractiveRandom ( [int]$Seed )
{
function Mod ( [int]$X, [int]$M = 1000000000 ) { ( $X % $M + $M ) % $M }
If ( $Seed )
{
$R = New-Object int[] 55
$N1 = 55 - 1
$N2 = ( $N1 + 34 ) % 55
$R[$N1] = $Seed
$R[$N2] = 1
ForEach ( $x in 2..(55-1) )
{
$N0, $N1, $N2 = $N1, $N2, ( ( $N2 + 34 ) % 55 )
$R[$N2] = Mod ( $R[$N0] - $R[$N1] )
}
$i = -55 - 1
$j = -24 - 1
ForEach ( $x in 55..219 )
{
$i = ++$i % 55
$j = ++$j % 55
$R[$i] = Mod ( $R[$i] - $R[$j] )
}
$Script:RandomRing = $R
$Script:RandomIndex = $i
}
$i = $Script:RandomIndex = ++$Script:RandomIndex % 55
$j = ( $i + 55 - 24 ) % 55
return ( $Script:RandomRing[$i] = Mod ( $Script:RandomRing[$i] - $Script:RandomRing[$j] ) )
}
Get-SubtractiveRandom 292929
Get-SubtractiveRandom
Get-SubtractiveRandom
Get-SubtractiveRandom
Get-SubtractiveRandom
</lang>
{{out}}
<pre>
467478574
512932792
539453717
20349702
615542081
</pre>