Subtractive generator: Difference between revisions
Content added Content deleted
Line 1,065: | Line 1,065: | ||
506003769 |
506003769 |
||
380969305</pre> |
380969305</pre> |
||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
Note that `subrand` as defined here returns the object representing |
|||
the current state of the PRNG, with .x set to the most recent PRN. |
|||
Note also that because jq does not support forward declarations |
|||
except insofar as a subfunction may call its parent (or grandparent, etc), |
|||
we have defined `subrand` as an accessible subfunction of `subrandSeed`. |
|||
<lang jq># input: state |
|||
# output: updated state |
|||
# If $p is null, then just call `subrand`, which sets .x as the PRN. |
|||
def subrandSeed($p): |
|||
def subrand: |
|||
if (.si == .sj) then subrandSeed(0) else . end |
|||
| .si |= (if . == 0 then 54 else . - 1 end) |
|||
| .sj |= (if . == 0 then 54 else . - 1 end) |
|||
| .mod as $mod |
|||
| .x = ((.state[.si] - .state[.sj]) | if . < 0 then . + $mod else . end) |
|||
| .state[.si] = .x ; |
|||
if $p == null then subrand |
|||
else |
|||
.p = $p |
|||
| .p2 = 1 |
|||
| .state[0] = ($p % .mod) |
|||
| .j = 21 |
|||
| reduce range(1; 55) as $i (.; |
|||
if .j >= 55 then .j += -55 else . end |
|||
| .state[.j] = .p2 |
|||
| .p2 = .p - .p2 |
|||
| if .p2 < 0 then .p2 = .p2 + .mod else . end |
|||
| .p = .state[.j] |
|||
| .j += 21) |
|||
| .si = 0 |
|||
| .sj = 24 |
|||
| reduce range(1; 166) as $i (.; subrand) |
|||
end; |
|||
def subrand: |
|||
subrandSeed(null); |
|||
{ mod: 1e9, |
|||
state: [range(0;55)|0], |
|||
si: 0, |
|||
sj: 0 } |
|||
| subrandSeed(292929) |
|||
| foreach range(0; 10) as $i (.; |
|||
subrand; |
|||
"r[\($i+220)] = \(.x)")</lang> |
|||
{{out}} |
|||
<pre> |
|||
r[220] = 467478574 |
|||
r[221] = 512932792 |
|||
r[222] = 539453717 |
|||
r[223] = 20349702 |
|||
r[224] = 615542081 |
|||
r[225] = 378707948 |
|||
r[226] = 933204586 |
|||
r[227] = 824858649 |
|||
r[228] = 506003769 |
|||
r[229] = 380969305 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |