Pseudo-random numbers/Xorshift star: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Haskell}}: added solution)
m (syntax highlighting fixup automation)
Line 87:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T XorShiftStar
UInt64 state
 
Line 113:
L 100'000
hist[Int(random_gen.next_float() * 5)]++
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 127:
=={{header|Ada}}==
Raises Unseeded_Error exception if state is not initialized before generating a pseudo-random value.
<langsyntaxhighlight Adalang="ada">with Interfaces; use Interfaces;
with Ada.Text_IO; use Ada.Text_IO;
 
Line 179:
 
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 198:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Will generate a runtime error if state is not initialised before use.
<langsyntaxhighlight lang="algol68">BEGIN # generate some pseudo random numbers using Xorshift star #
# note that although LONG INT is 64 bits in Algol 68G, LONG BITS is longer than 64 bits #
LONG BITS state;
Line 236:
print( ( newline ) )
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 248:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdint.h>
#include <stdio.h>
Line 299:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 315:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <cstdint>
#include <iostream>
Line 368:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 384:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 433:
writeln(i, ": ", v);
}
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 451:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Xorshift_star;
 
Line 523:
 
{$IFNDEF UNIX} Readln; {$ENDIF}
end.</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
===The Functions===
<langsyntaxhighlight lang="fsharp">
// Xorshift star. Nigel Galloway: August 14th., 2020
let fN=(fun(n:uint64)->n^^^(n>>>12))>>(fun n->n^^^(n<<<25))>>(fun n->n^^^(n>>>27))
let Xstar32=Seq.unfold(fun n->let n=fN n in Some(uint32((n*0x2545F4914F6CDD1DUL)>>>32),n))
let XstarF n=Xstar32 n|>Seq.map(fun n->(float n)/4294967296.0)
</syntaxhighlight>
</lang>
===The Tasks===
<langsyntaxhighlight lang="fsharp">
Xstar32 1234567UL|>Seq.take 5|>Seq.iter(printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 545:
3809424708
</pre>
<langsyntaxhighlight lang="fsharp">
XstarF 987654321UL|>Seq.take 100000|>Seq.countBy(fun n->int(n*5.0))|>Seq.iter(printf "%A");printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 554:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors kernel literals math math.statistics
prettyprint sequences ;
 
Line 584:
987654321 >>state
100,000 [ dup next-float 5 * >integer ] replicate nip
histogram .</langsyntaxhighlight>
{{out}}
<pre>
Line 597:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 641:
fmt.Printf(" %d : %d\n", i, counts[i])
}
}</langsyntaxhighlight>
 
{{out}}
Line 663:
Implement given algorithm as an instance of <code>RandomGen</code> class.
 
<langsyntaxhighlight lang="haskell">import Data.Bits
import Data.Word
import System.Random
Line 683:
randoms' = unfoldr (pure . next)
 
toFloat n = fromIntegral n / (2^32 - 1)</langsyntaxhighlight>
 
Direct usage of generator:
Line 712:
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">public class XorShiftStar {
private static final long MAGIC = Long.parseUnsignedLong("2545F4914F6CDD1D", 16);
private long state;
Line 758:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 774:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">const mask32 = (0x1 << 32) - 1
const CONST = 0x2545F4914F6CDD1D
Line 811:
 
testXorShiftStar()
</langsyntaxhighlight>{{out}}
<pre>
3540625527
Line 823:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import kotlin.math.floor
 
class XorShiftStar {
Line 871:
println("${iv.index}: ${iv.value}")
}
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 887:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function create()
local g = {
magic = 0x2545F4914F6CDD1D,
Line 927:
for i,v in pairs(counts) do
print(i..': '..v)
end</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 942:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils, tables
 
const C = 0x2545F4914F6CDD1Du64
Line 978:
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 990:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
no warnings 'portable';
Line 1,027:
$rng = Xorshift_star->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,046:
As per [[Pseudo-random_numbers/PCG32#Phix]], resorting to mpfr/gmp
{{libheader|Phix/mpfr}}
<!--<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,091:
<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,103:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">mask64 = (1 << 64) - 1
mask32 = (1 << 32) - 1
const = 0x2545F4914F6CDD1D
Line 1,142:
for i in range(100_000):
hist[int(random_gen.next_float() *5)] += 1
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 1,158:
Raku does not have unsigned Integers at this time (Integers are arbitrary sized) so use explicit bit masks during bitwise operations. All constants are encapsulated inside the class.
 
<syntaxhighlight lang="raku" perl6line>class Xorshift-star {
has $!state;
 
Line 1,188:
say "\nSeed: default; first five Int values";
$rng = Xorshift-star.new;
.say for $rng.next-int xx 5;</langsyntaxhighlight>
{{out}}
<pre>Seed: 1234567; first five Int values
Line 1,209:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program generates pseudo─random numbers using the XOR─shift─star method. */
numeric digits 200 /*ensure enough decimal digs for mult. */
parse arg n reps pick seed1 seed2 . /*obtain optional arguments from the CL*/
Line 1,258:
xor: parse arg a, b; $= /*perform a bit─wise XOR. */
do !=1 for length(a); $= $ || (substr(a,!,1) && substr(b,!,1) )
end /*!*/; return $</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,280:
=={{header|Ruby}}==
Using Ruby 3.0 end-les method def:
<langsyntaxhighlight lang="ruby">class Xorshift_star
MASK64 = (1 << 64) - 1
MASK32 = (1 << 32) - 1
Line 1,305:
tally = Hash.new(0)
100_000.times{ tally[(random_gen.next_float*5).floor] += 1 }
puts tally.sort.map{|ar| ar.join(": ") }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,322:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">class Xorshift_star(state) {
 
define (
Line 1,348:
var rng = Xorshift_star(987654321)
var histogram = Bag(1e5.of { floor(5*rng.next_float) }...)
histogram.pairs.sort.each { .join(": ").say }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,364:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
struct XorshiftStar {
Line 1,408:
}
 
print(counts)</langsyntaxhighlight>
 
{{out}}
Line 1,423:
{{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.fromBaseString("2545F4914F6CDD1D", 16)
Line 1,458:
}
System.print("\nThe counts for 100,000 repetitions are:")
for (i in 0..4) System.print(" %(i) : %(counts[i])")</langsyntaxhighlight>
 
{{out}}
10,333

edits