Pseudo-random numbers/PCG32: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 75:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T PCG32
UInt64 state, inc
 
Line 104:
L 100'000
hist[Int(random_gen.next_float() * 5)]++
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 118:
=={{header|Ada}}==
Ada solution using a package to encapsulate the PCG32 algorithm.
<langsyntaxhighlight Adalang="ada">with Interfaces; use Interfaces;
 
package random_pcg32 is
Line 125:
procedure Seed (seed_state : Unsigned_64; seed_sequence : Unsigned_64);
end random_pcg32;
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight Adalang="ada">package body random_pcg32 is
 
State : Unsigned_64 := 0;
Line 185:
 
end random_pcg32;
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io; use ADa.Text_io;
with Interfaces; use Interfaces;
with random_pcg32; use random_pcg32;
Line 212:
end Main_P;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 230:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68">BEGIN # generate some pseudo random numbers using PCG32 #
# note that although LONG INT is 64 bits in Algol 68G, LONG BITS is longer than 64 bits #
LONG BITS state := LONG 16r853c49e6748fea9b;
Line 286:
print( ( newline ) )
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 299:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdint.h>
#include <stdio.h>
Line 352:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>2707161783
Line 369:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <iostream>
 
Line 423:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>2707161783
Line 440:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 493:
writeln(" ", i, " : ", v);
}
}</langsyntaxhighlight>
{{out}}
<pre>2707161783
Line 514:
{{libheader| System.Generics.Collections}}
{{Trans|Python}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program PCG32_test;
 
Line 635:
Readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 654:
=={{header|F_Sharp|F#}}==
===The Functions===
<langsyntaxhighlight lang="fsharp">
// PCG32. Nigel Galloway: August 13th., 2020
let N=6364136223846793005UL
Line 660:
let pcg32=Seq.unfold(fun(n,g)->let rot,xs=uint32(g>>>59),uint32(((g>>>18)^^^g)>>>27) in Some(uint32((xs>>>(int rot))|||(xs<<<(-(int rot)&&&31))),(n,g*N+n)))
let pcgFloat n=pcg32 n|>Seq.map(fun n-> (float n)/4294967296.0)
</syntaxhighlight>
</lang>
===The Tasks===
<langsyntaxhighlight lang="fsharp">
pcg32(seed 42UL 54UL)|>Seq.take 5|>Seq.iter(printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 673:
3215226955
</pre>
<langsyntaxhighlight lang="fsharp">
pcgFloat(seed 987654321UL 1UL)|>Seq.take 100000|>Seq.countBy(fun n->int(n*5.0))|>Seq.iter(printf "%A");printfn ""
</syntaxhighlight>
</lang>
<pre>
(2, 20115)(3, 19809)(0, 20049)(4, 20005)(1, 20022)
Line 682:
{{trans|Python}}
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: accessors kernel locals math math.bitwise math.statistics
prettyprint sequences ;
 
Line 714:
987654321 1 [ seed ] keepdd
100,000 [ dup next-float 5 * >integer ] replicate nip
histogram .</langsyntaxhighlight>
{{out}}
<pre>
Line 727:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 777:
fmt.Printf(" %d : %d\n", i, counts[i])
}
}</langsyntaxhighlight>
 
{{out}}
Line 799:
Implement given algorithm as an instance of <code>RandomGen</code> class.
 
<langsyntaxhighlight lang="haskell">import Data.Bits
import Data.Word
import System.Random
Line 826:
randoms' g = unfoldr (pure . next) g
 
toFloat n = fromIntegral n / (2^32 - 1)</langsyntaxhighlight>
 
Direct usage of generator:
Line 847:
 
Implementation:
<langsyntaxhighlight Jlang="j">PCG32GEN=: {{
g=. cocreate''
'state0__g seq__g'=. m
Line 873:
}}
 
next_float=: %&(2^32)</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> 42 54 PCG32GEN ^:(1+i.5)''
2707161776 2068313120 3122475824 2211639955 3215226955
(~.,. #/.~) <.5*next_float 987654321 1 PCG32GEN^:(1+i.1e5) ''
Line 884:
0 20049
4 20005
1 20022</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">public class PCG32 {
private static final long N = 6364136223846793005L;
 
Line 938:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>2707161783
Line 955:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">const mask32, CONST = 0xffffffff, UInt(6364136223846793005)
 
mutable struct PCG32
Line 1,001:
 
testPCG32()
</langsyntaxhighlight>{{out}}
<pre>
2707161783
Line 1,015:
{{trans|C++}}
Requires the experimental unsigned feature for integer types
<langsyntaxhighlight lang="scala">import kotlin.math.floor
 
class PCG32 {
Line 1,068:
println(" %d : %d".format(iv.index, iv.value))
}
}</langsyntaxhighlight>
{{out}}
<pre>2707161783
Line 1,085:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function uint32(n)
return n & 0xffffffff
end
Line 1,137:
for i=1,5 do
print(" " .. (i - 1) .. ": " .. counts[i])
end</langsyntaxhighlight>
{{out}}
<pre>2707161783
Line 1,153:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils, tables
 
const N = 6364136223846793005u64
Line 1,188:
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,200:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,245:
$rng = PCG32->new(seed => 987654321, incr => 1);
$h{int 5 * $rng->next_float}++ for 1 .. 100_000;
say "$_ $h{$_}" for sort keys %h;</langsyntaxhighlight>
{{out}}
<pre>Seed: 42, Increment: 54, first 5 values:
Line 1,266:
Phix atoms are limited to 53/64 bits of precision, however (given the above) this task would need 128 bits.<br>
First, for comparison only, this is the usual recommended native approach for this genre of task (different output)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"NB: These are not expected to match the task spec!\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">set_rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">42</span><span style="color: #0000FF;">)</span>
Line 1,278:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,290:
</pre>
To meet the spec, similar to the Delphi and Wren entries, we resort to using mpfr/gmp, but it is a fair bit longer than the above, and almost certainly slower, not that there is anywhere near enough work being done here to make that measureable.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,344:
<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,358:
 
===Python: As class===
<langsyntaxhighlight lang="python">mask64 = (1 << 64) - 1
mask32 = (1 << 32) - 1
CONST = 6364136223846793005
Line 1,404:
for i in range(100_000):
hist[int(random_gen.next_float() *5)] += 1
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 1,416:
===Python: As generator===
 
<langsyntaxhighlight lang="python">def pcg32(seed_state=None, seed_sequence=None, as_int=True):
def next_int():
"return random 32 bit unsigned int"
Line 1,448:
for i in islice(pcg32(987654321, 1, as_int=False), 100_000):
hist[int(i * 5)] += 1
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 1,464:
Raku does not have unsigned Integers at this time (Integers are arbitrary sized) so use explicit bit masks during bitwise operations.
 
<syntaxhighlight lang="raku" perl6line>class PCG32 {
has $!state;
has $!incr;
Line 1,505:
say "\nSeed: default, Increment: default; first five Int values:";
$rng = PCG32.new;
.say for $rng.next-int xx 5;</langsyntaxhighlight>
{{out}}
<pre>Seed: 42, Increment: 54; first five Int values:
Line 1,529:
DON'T use Rexx, however, for this type of problem unless you take the time spent
for some Java coffees!
<langsyntaxhighlight lang="rexx">Numeric Digits 40
N = 6364136223846793005
state = x2d('853c49e6748fea9b',16)
Line 1,819:
b=b||bits.c
End
Return b </langsyntaxhighlight>
{{out}}
<pre>
Line 1,838:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight Rubylang="ruby">class PCG32
MASK64 = (1 << 64) - 1
MASK32 = (1 << 32) - 1
Line 1,872:
random_gen.seed(987654321, 1)
p 100_000.times.each{(random_gen.next_float * 5).floor}.tally.sort.to_h
</syntaxhighlight>
</lang>
{{out}}
<pre>2707161783
Line 1,886:
{{trans|Ruby}}
 
<langsyntaxhighlight Schemelang="scheme">(import (scheme small) (srfi 33))
 
(define PCG-DEFAULT-MULTIPLIER 6364136223846793005)
Line 1,954:
(newline)
(lp (+ i 1))))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,973:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">class PCG32(seed, incr) {
 
has state
Line 2,009:
var rng = PCG32(seed: 987654321, incr: 1)
var histogram = Bag(1e5.of { floor(5*rng.next_float) }...)
histogram.pairs.sort.each { .join(": ").say }</langsyntaxhighlight>
{{out}}
<pre>
Line 2,024:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">type pcg32 = LargeWord.word * LargeWord.word
 
local
Line 2,046:
Word.fromLarge (state >> 0w59)),
(state * m + inc, inc))
end</langsyntaxhighlight>
;Test code<nowiki>:</nowiki>
<langsyntaxhighlight lang="sml">fun test1 (rand, state) =
(print (Word32.fmt StringCvt.DEC rand ^ "\n"); state)
 
Line 2,072:
 
val _ = doTimes (test2 o pcg32Random, 100000, pcg32Init (987654321, 1))
val () = print ("\n" ^ ((String.concatWith ", " o test2res) ()) ^ "\n")</langsyntaxhighlight>
{{out}}
<pre>2707161783
Line 2,085:
{{trans|C}}
uBasic/4tH only supports signed integers - so floating point is out of the question. It also requires clipping some integers to 32 bits in order to make this work.
<syntaxhighlight lang="text">' ** NOTE: this requires a 64-bit uBasic. **
 
If Info("wordsize") < 64 Then Print "This program requires a 64-bit uBasic" : End
Line 2,118:
s = s + a@
Proc _PCG32Int
Return</langsyntaxhighlight>
{{Out}}
<pre>2707161783
Line 2,131:
{{libheader|Wren-big}}
As Wren doesn't have a 64-bit integer type, we use BigInt instead.
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt
 
var Const = BigInt.new("6364136223846793005")
Line 2,173:
}
System.print("\nThe counts for 100,000 repetitions are:")
for (i in 0..4) System.print(" %(i) : %(counts[i])")</langsyntaxhighlight>
 
{{out}}
10,333

edits