Jump to content

Pseudo-random numbers/Combined recursive generator MRG32k3a: Difference between revisions

→‎{{header|jq}}: include "MRG32k3a"
(→‎{{header|jq}}: include "MRG32k3a")
 
Line 839:
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq''' provided the
MRG32k3a module (minus its declaration) is inlined.
 
The jq module ''MRG32k3a'' is available at [[:Category:Jq/MRG32k3a.jq | MRG32k3a.jq]].
 
<syntaxhighlight lang="jq">
include "MRG32k3a" {search: "."}; # see above
# constants
def A1: [0, 1403580, -810728];
def M1: 4294967087; # pow(2;32) - 209
def A2: [527612, 0, -1370589];
def M2: 4294944443; # pow(2;32) - 22853
def D: M1 + 1;
 
# Python-style modulus
def Mod($x; $y):
def abs: if . < 0 then - . else . end;
($x % $y) as $m
| if $m < 0 then $m + ($y|abs) else $m end;
 
def MRG32k3a: {x1: [0, 0, 0], x2: [0, 0, 0] };
 
def seed($seedState):
if $seedState <= 0 or $seedState >= D
then "Argument of seed must be in the range (0, \(D))" | error
else {x1: [$seedState, 0, 0]} | .x2 = .x1
end ;
 
# Input: {x1, x2} as for MRG32k31
# Output: {x1, x2, nextInt}
def nextInt:
Mod(A1[0]*.x1[0] + A1[1]*.x1[1] + A1[2]*.x1[2]; M1) as $x1i
| Mod(A2[0]*.x2[0] + A2[1]*.x2[1] + A2[2]*.x2[2]; M2) as $x2i
| .x1 = [$x1i, .x1[0], .x1[1]] # keep last three
| .x2 = [$x2i, .x2[0], .x2[1]] # keep last three
| .nextInt = Mod($x1i - $x2i; M1) + 1 ;
 
def nextFloat: nextInt | .nextFloat = (.nextInt / D);
 
def task1:
Line 888 ⟶ 861:
task2(100000)
</syntaxhighlight>
 
{{output}}
<pre>
2,502

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.