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

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
m (syntax highlighting fixup automation)
Line 73:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V a1 = [Int64(0), 1403580, -810728]
V m1 = Int64(2) ^ 32 - 209
V a2 = [Int64(527612), 0, -1370589]
Line 112:
L 100'000
hist[Int(random_gen.next_float() * 5)]++
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 125:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">package MRG32KA is
type I64 is range -2**63..2**63 - 1;
m1 : constant I64 := 2**32 - 209;
Line 136:
function Next_Float return Long_Float;
end MRG32KA;
</syntaxhighlight>
</lang>
 
<syntaxhighlight lang="ada">
<lang Ada>
package body MRG32KA is
Line 192:
 
end MRG32KA;
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with mrg32ka; use mrg32ka;
 
Line 219:
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 232:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdint.h>
Line 317:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 333:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <iostream>
 
Line 407:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 423:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 495:
writeln(i, ": ", v);
}
}</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 510:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays kernel math math.order math.statistics
math.vectors prettyprint sequences ;
 
Line 535:
 
987654321 seed 100,000 [ next-float 5 * >integer ] replicate
2nip histogram .</langsyntaxhighlight>
{{out}}
<pre>
Line 549:
{{trans|uBasic/4tH}}
{{works with|4tH v3.64}}
<langsyntaxhighlight lang="forth">6 array (seed) \ holds the seed
6 array (gens) \ holds the generators
\ set up constants
Line 592:
;
 
test</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 608:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 675:
fmt.Printf(" %d : %d\n", i, counts[i])
}
}</langsyntaxhighlight>
 
{{out}}
Line 695:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List
 
randoms :: Int -> [Int]
Line 710:
m2 = 2^32 - 22853
 
randomsFloat = map ((/ (2^32 - 208)) . fromIntegral) . randoms</langsyntaxhighlight>
 
<pre>*Main> take 5 $ randoms 1234567
Line 720:
 
=== As a RandomGen instanse ===
<langsyntaxhighlight lang="haskell">import System.Random
 
newtype MRG32k3a = MRG32k3a ([Int],[Int])
Line 737:
m2 = 2^32 - 22853
 
split _ = error "MRG32k3a is not splittable"</langsyntaxhighlight>
 
In this case the sequence or numbers differs from direct unfolding, due to internal uniform shuffling.
Line 750:
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">public class App {
private static long mod(long x, long y) {
long m = x % y;
Line 819:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 834:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const a1 = [0, 1403580, -810728]
const m1 = 2^32 - 209
const a2 = [527612, 0, -1370589]
Line 868:
end
foreach(p -> print(p[1], ": ", p[2], " "), enumerate(hist))
</langsyntaxhighlight>{{out}}
<pre>
1459213977
Line 880:
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">import kotlin.math.floor
 
fun mod(x: Long, y: Long): Long {
Line 949:
println("${iv.index}: ${iv.value}")
}
}</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 964:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, math, sequtils, strutils, tables
 
const
Line 1,011:
for _ in 1..100_000:
counts.inc int(gen.nextFloat() * 5)
echo sorted(toSeq(counts.pairs)).mapIt($it[0] & ": " & $it[1]).join(", ")</langsyntaxhighlight>
 
{{out}}
Line 1,024:
=={{header|Pari/GP}}==
Pretty straightforward translation from the directions. Used column/vector multiplication (essentially he dot product) instead of the more tedious form given in the definition of x1i and x2i; rationals (t_FRAC) used in place of floating-point since GP lacks floating-point.
<langsyntaxhighlight lang="parigp">a1 = [0, 1403580, -810728];
m1 = 2^32-209;
a2 = [527612, 0, -1370589];
Line 1,042:
vector(5,i,next_int())
seed(987654321);
v=vector(5); for(i=1,1e5, v[next_float()*5\1+1]++); v</langsyntaxhighlight>
{{out}}
<pre>%1 = [1459213977, 2827710106, 4245671317, 3877608661, 2595287583]
Line 1,048:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,087:
$rng = MRG32k3a->new( seed => 987654321 );
$h{int 5 * $rng->next_float}++ for 1..100_000;
say "$_ $h{$_}" for sort keys %h;</langsyntaxhighlight>
{{out}}
<pre>Seed: 1234567, first 5 values:
Line 1,104:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span>
Line 1,149:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">r</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,161:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python"># Constants
a1 = [0, 1403580, -810728]
m1 = 2**32 - 209
Line 1,207:
for i in range(100_000):
hist[int(random_gen.next_float() *5)] += 1
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 1,223:
All constants are encapsulated within the class.
 
<syntaxhighlight lang="raku" perl6line>class MRG32k3a {
has @!x1;
has @!x2;
Line 1,259:
say "\nSeed: default; first five Int values:";
$rng = MRG32k3a.new;
.say for $rng.next-int xx 5;</langsyntaxhighlight>
{{out}}
<pre>Seed: 1234567; first five Int values:
Line 1,280:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang="ruby">def mod(x, y)
m = x % y
if m < 0 then
Line 1,347:
counts.each_with_index { |v,i|
print i, ": ", v, "\n"
}</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 1,361:
4: 19931</pre>
===Mimicking the Pseudo-code===
<langsyntaxhighlight Rubylang="ruby"># Constants
# First generator
A1 = [0, 1403580, -810728]
Line 1,401:
random_gen.seed(987654321)
p 100_000.times.map{(random_gen.next_float() * 5).floor}.tally.sort.to_h
</syntaxhighlight>
</lang>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">class MRG32k3a(seed) {
 
define(
Line 1,436:
var rng = MRG32k3a(seed: 987654321)
var freq = 100_000.of { rng.next_float * 5 -> int }.freq
freq.sort.each_2d {|k,v| say "#{k} #{v}" }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,458:
{{trans|C}}
Since uBasic/4tH has no floating point support, only the integer part of the task can be implemented.
<syntaxhighlight lang="text">@(0) = 0 ' First generator
@(1) = 1403580
@(2) = -810728
Line 1,518:
@(9) = b@
 
Return (c@ + 1)</langsyntaxhighlight>
{{out}}
<pre>1459213977
Line 1,532:
=={{header|Wren}}==
{{trans|Python}}
<langsyntaxhighlight lang="ecmascript">// constants
var A1 = [0, 1403580, -810728]
var M1 = 2.pow(32) - 209
Line 1,581:
}
System.print("\nThe counts for 100,000 repetitions are:")
for (i in 0..4) System.print(" %(i) : %(counts[i])")</langsyntaxhighlight>
 
{{out}}
10,327

edits