Random sentence from book: Difference between revisions

→‎{{header|jq}}: include "MRG32k3a";
(→‎{{Header|AutoHotkey}}: added missing jq header)
(→‎{{header|jq}}: include "MRG32k3a";)
 
Line 8,942:
 
In the following, the MRG32k3a combined recursive PRNG is used to generate random numbers;
the code is available at [[:Category:Jq/MRG32k3a.jq]].
theSee code is borrowed fromalso [[Pseudo-random_numbers/Combined_recursive_generator_MRG32k3a#jq|RosettaCode.org]].
 
Invocation of jq is along the lines of
Line 8,949 ⟶ 8,950:
</pre>
<syntaxhighlight lang="jq">
include "MRG32k3a" {search: "."}; # see above
 
### Generic functions
 
def sum(stream): reduce stream as $s (0; . +$s);
 
# 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;
 
### The MRG32k3a PRNG
# 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;
 
# Initialize the PRNG
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);
 
### Random sentences
2,507

edits