Pseudo-random numbers/Xorshift star: Difference between revisions

Add Rust implementation
(Add Rust implementation)
 
(7 intermediate revisions by 7 users not shown)
Line 27:
/* Let u64 denote an unsigned 64 bit integer type. */
/* Let u32 denote an unsigned 32 bit integer type. */
 
 
class Xorshift_star
u64 state /* Must be seeded to non-zero initial value */
u64 const = HEX '2545F4914F6CDD1D'
 
method seed(u64 num):
state = num
Line 87 ⟶ 86:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T XorShiftStar
UInt64 state
 
Line 113 ⟶ 112:
L 100'000
hist[Int(random_gen.next_float() * 5)]++
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 127 ⟶ 126:
=={{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 ⟶ 178:
 
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 198 ⟶ 197:
{{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 ⟶ 235:
print( ( newline ) )
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 248 ⟶ 247:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdint.h>
#include <stdio.h>
Line 299 ⟶ 298:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 315 ⟶ 314:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <cstdint>
#include <iostream>
Line 368 ⟶ 367:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 384 ⟶ 383:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 433 ⟶ 432:
writeln(i, ": ", v);
}
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 446 ⟶ 445:
3: 20031
4: 20007</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Xorshift_star;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils,
System.Math;
 
type
TXorshiftStar = record
private
state: uint64;
const
k = $2545F4914F6CDD1D;
public
constructor Create(aState: uint64);
procedure Seed(aState: uint64);
function NextInt: uint32;
function NextFloat: Extended;
end;
 
{ TXorshiftStar }
 
constructor TXorshiftStar.Create(aState: uint64);
begin
Seed(aState);
end;
 
function TXorshiftStar.NextFloat: Extended;
begin
Result := NextInt() / $100000000;
end;
 
function TXorshiftStar.NextInt: uint32;
var
x: UInt64;
begin
x := state;
x := x xor (x shr 12);
x := x xor (x shl 25);
x := x xor (x shr 27);
state := x;
Result := uint32((x * k) shr 32);
end;
 
procedure TXorshiftStar.Seed(aState: uint64);
begin
state := aState;
end;
 
begin
var randomGen := TXorshiftStar.Create(1234567);
 
for var i := 0 to 4 do
writeln(randomGen.NextInt);
 
var counts := [0, 0, 0, 0, 0];
randomGen.seed(987654321);
 
for var i := 1 to 100000 do
begin
var j := Floor(randomGen.nextFloat() * 5);
inc(counts[j]);
end;
 
writeln(#10'The counts for 100,000 repetitions are:');
 
for var i := 0 to 4 do
writeln(format(' %d : %d', [i, counts[i]]));
 
{$IFNDEF UNIX} Readln; {$ENDIF}
end.</syntaxhighlight>
 
=={{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 467 ⟶ 544:
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 476 ⟶ 553:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors kernel literals math math.statistics
prettyprint sequences ;
 
Line 506 ⟶ 583:
987654321 >>state
100,000 [ dup next-float 5 * >integer ] replicate nip
histogram .</langsyntaxhighlight>
{{out}}
<pre>
Line 519 ⟶ 596:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 563 ⟶ 640:
fmt.Printf(" %d : %d\n", i, counts[i])
}
}</langsyntaxhighlight>
 
{{out}}
Line 580 ⟶ 657:
4 : 20007
</pre>
 
=={{header|Haskell}}==
 
Implement given algorithm as an instance of <code>RandomGen</code> class.
 
<syntaxhighlight lang="haskell">import Data.Bits
import Data.Word
import System.Random
import Data.List
 
newtype XorShift = XorShift Word64
 
instance RandomGen XorShift where
next (XorShift state) = (out newState, XorShift newState)
where
newState = (\z -> z `xor` (z `shiftR` 27)) .
(\z -> z `xor` (z `shiftL` 25)) .
(\z -> z `xor` (z `shiftR` 12)) $ state
out x = fromIntegral $ (x * 0x2545f4914f6cdd1d) `shiftR` 32
 
split _ = error "XorShift is not splittable"
 
randoms' :: RandomGen g => g -> [Int]
randoms' = unfoldr (pure . next)
 
toFloat n = fromIntegral n / (2^32 - 1)</syntaxhighlight>
 
Direct usage of generator:
<pre>*Main> mapM_ print $ take 5 $ randoms' (XorShift 1234567)
3540625527
2750739987
4037983143
1993361440
3809424708
 
*Main> let hist = map length . group . sort
*Main> hist . take 100000 $ (floor . (*5) . toFloat) <$> (randoms' (XorShift 987654321))
[20103,19922,19937,20031,20007]</pre>
 
Using <code>Random</code> class gives different results due to internal shuffling:
<pre>*Main> mapM_ print $ take 5 $ randoms (XorShift 1234567)
2750739987
1993361440
1794978290
626183142
2911384526
 
*Main> let hist = map length . group . sort
*Main> hist . take 100000 $ (floor . (*5)) <$> (randoms (XorShift 987654321) :: [Float])
[20263,19783,19949,19957,20048]</pre>
 
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">public class XorShiftStar {
private static final long MAGIC = Long.parseUnsignedLong("2545F4914F6CDD1D", 16);
private long state;
Line 629 ⟶ 757:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 645 ⟶ 773:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">const mask32 = (0x1 << 32) - 1
const CONST = 0x2545F4914F6CDD1D
Line 682 ⟶ 810:
 
testXorShiftStar()
</langsyntaxhighlight>{{out}}
<pre>
3540625527
Line 694 ⟶ 822:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import kotlin.math.floor
 
class XorShiftStar {
Line 742 ⟶ 870:
println("${iv.index}: ${iv.value}")
}
}</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 758 ⟶ 886:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function create()
local g = {
magic = 0x2545F4914F6CDD1D,
Line 798 ⟶ 926:
for i,v in pairs(counts) do
print(i..': '..v)
end</langsyntaxhighlight>
{{out}}
<pre>3540625527
Line 813 ⟶ 941:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils, tables
 
const C = 0x2545F4914F6CDD1Du64
Line 849 ⟶ 977:
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 861 ⟶ 989:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
no warnings 'portable';
Line 898 ⟶ 1,026:
$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 916 ⟶ 1,044:
=={{header|Phix}}==
As per [[Pseudo-random_numbers/PCG32#Phix]], resorting to mpfr/gmp
<lang {{libheader|Phix>include /mpfr.e}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
mpz const = mpz_init("0x2545F4914F6CDD1D"),
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
state = mpz_init(),
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
b64 = mpz_init("0x10000000000000000"), -- (truncate to 64 bits)
<span style="color: #004080;">mpz</span> <span style="color: #000000;">cmult</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0x2545F4914F6CDD1D"</span><span style="color: #0000FF;">),</span>
b32 = mpz_init("0x100000000"), -- (truncate to 32 bits)
<span style="color: #000000;">state</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
tmp = mpz_init(),
<span style="color: #000000;">b64</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0x10000000000000000"</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (truncate to 64 bits)</span>
x = mpz_init()
<span style="color: #000000;">b32</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0x100000000"</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (truncate to 32 bits)</span>
<span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
procedure seed(integer num)
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
mpz_set_si(state,num)
end procedure
<span style="color: #008080;">procedure</span> <span style="color: #000000;">seed</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">state</span><span style="color: #0000FF;">,</span><span style="color: #000000;">num</span><span style="color: #0000FF;">)</span>
function next_int()
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
mpz_set(x,state) -- x := state
mpz_tdiv_q_2exp(tmp, x, 12) -- tmp := trunc(x/2^12)
<span style="color: #008080;">function</span> <span style="color: #000000;">next_int</span><span style="color: #0000FF;">()</span>
mpz_xor(x, x, tmp) -- x := xor_bits(x,tmp)
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">state</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x := state</span>
mpz_mul_2exp(tmp, x, 25) -- tmp := x * 2^25.
<span style="color: #7060A8;">mpz_tdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- tmp := trunc(x/2^12)</span>
mpz_xor(x, x, tmp) -- x := xor_bits(x,tmp)
<span style="color: #7060A8;">mpz_xor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x := xor_bits(x,tmp)</span>
mpz_fdiv_r(x, x, b64) -- x := remainder(x,b64)
<span style="color: #7060A8;">mpz_mul_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- tmp := x * 2^25.</span>
mpz_tdiv_q_2exp(tmp, x, 27) -- tmp := trunc(x/2^27)
<span style="color: #7060A8;">mpz_xor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x := xor_bits(x,tmp)</span>
mpz_xor(x, x, tmp) -- x := xor_bits(x,tmp)
<span style="color: #7060A8;">mpz_fdiv_r</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b64</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x := remainder(x,b64) </span>
mpz_fdiv_r(state, x, b64) -- state := remainder(x,b64)
<span style="color: #7060A8;">mpz_tdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">27</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- tmp := trunc(x/2^27)</span>
mpz_mul(x,x,const) -- x *= const
<span style="color: #7060A8;">mpz_xor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x := xor_bits(x,tmp)</span>
mpz_tdiv_q_2exp(x, x, 32) -- x := trunc(x/2^32)
<span style="color: #7060A8;">mpz_fdiv_r</span><span style="color: #0000FF;">(</span><span style="color: #000000;">state</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b64</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- state := remainder(x,b64) </span>
mpz_fdiv_r(x, x, b32) -- x := remainder(x,b32)
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cmult</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x *= cmult</span>
return mpz_get_atom(x)
<span style="color: #7060A8;">mpz_tdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x := trunc(x/2^32)</span>
end function
<span style="color: #7060A8;">mpz_fdiv_r</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b32</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- x := remainder(x,b32) </span>
 
<span style="color: #008080;">return</span> <span style="color: #7060A8;">mpz_get_atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
function next_float()
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return next_int() / (1 << 32)
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">next_float</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">next_int</span><span style="color: #0000FF;">()</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">#100000000</span>
seed(1234567)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
for i=1 to 5 do
printf(1,"%d\n",next_int())
<span style="color: #000000;">seed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1234567</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
seed(987654321)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">next_int</span><span style="color: #0000FF;">())</span>
sequence r = repeat(0,5)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for i=1 to 100000 do
<span style="color: #000000;">seed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">987654321</span><span style="color: #0000FF;">)</span>
r[floor(next_float()*5)+1] += 1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">100000</span> <span style="color: #008080;">do</span>
?r</lang>
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">next_float</span><span style="color: #0000FF;">()*</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
<span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">r</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 969 ⟶ 1,102:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">mask64 = (1 << 64) - 1
mask32 = (1 << 32) - 1
const = 0x2545F4914F6CDD1D
Line 1,008 ⟶ 1,141:
for i in range(100_000):
hist[int(random_gen.next_float() *5)] += 1
print(hist)</langsyntaxhighlight>
 
{{out}}
Line 1,024 ⟶ 1,157:
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,054 ⟶ 1,187:
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,075 ⟶ 1,208:
 
=={{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,124 ⟶ 1,257:
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,146 ⟶ 1,279:
=={{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,171 ⟶ 1,304:
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,185 ⟶ 1,318:
4: 20007
</pre>
 
=={{header|Rust}}==
{{trans|C++}}
<syntaxhighlight lang="Rust">
struct XorShiftStar {
magic: u64,
state: u64,
}
 
impl XorShiftStar {
fn new() -> Self {
Self {
magic: 0x2545_F491_4F6C_DD1D,
state: 0,
}
}
 
fn seed(&mut self, num: u64) {
self.state = num;
}
 
fn next_int(&mut self) -> u32 {
let mut x = self.state;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.state = x;
((x.wrapping_mul(self.magic)) >> 32) as u32
}
 
fn next_float(&mut self) -> f32 {
self.next_int() as f32 / (1u64 << 32) as f32
}
}
 
fn main() {
let mut rng = XorShiftStar::new();
rng.seed(1234567);
println!("{}", rng.next_int());
println!("{}", rng.next_int());
println!("{}", rng.next_int());
println!("{}", rng.next_int());
println!("{}", rng.next_int());
println!();
 
let mut counts = [0; 5];
rng.seed(987654321);
for _ in 0..100000 {
let j = (rng.next_float() * 5.0).floor() as usize;
counts[j] += 1;
}
for (i, count) in counts.iter().enumerate() {
println!("{}: {}", i, count);
}
}
</syntaxhighlight>
{{out}}
<pre>
3540625527
2750739987
4037983143
1993361440
3809424708
 
0: 20103
1: 19922
2: 19937
3: 20031
4: 20007
</pre>
 
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">class Xorshift_star(state) {
 
define (
Line 1,214 ⟶ 1,418:
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,227 ⟶ 1,431:
4: 20007
</pre>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">import Foundation
 
struct XorshiftStar {
private let magic: UInt64 = 0x2545F4914F6CDD1D
private var state: UInt64
 
init(seed: UInt64) {
state = seed
}
mutating func nextInt() -> UInt64 {
state ^= state &>> 12
state ^= state &<< 25
state ^= state &>> 27
 
return (state &* magic) &>> 32
}
 
mutating func nextFloat() -> Float {
return Float(nextInt()) / Float(1 << 32)
}
}
 
extension XorshiftStar: RandomNumberGenerator, IteratorProtocol, Sequence {
mutating func next() -> UInt64 {
return nextInt()
}
 
mutating func next() -> UInt64? {
return nextInt()
}
}
 
for (i, n) in XorshiftStar(seed: 1234567).lazy.enumerated().prefix(5) {
print("\(i): \(n)")
}
 
var gen = XorshiftStar(seed: 987654321)
var counts = [Float: Int]()
 
for _ in 0..<100_000 {
counts[floorf(gen.nextFloat() * 5), default: 0] += 1
}
 
print(counts)</syntaxhighlight>
 
{{out}}
 
<pre>0: 3540625527
1: 2750739987
2: 4037983143
3: 1993361440
4: 3809424708
[1.0: 19922, 4.0: 20007, 3.0: 20031, 2.0: 19937, 0.0: 20103]</pre>
 
=={{header|Wren}}==
Line 1,232 ⟶ 1,493:
{{libheader|Wren-big}}
As Wren doesn't have a 64-bit integer type, we use BigInt instead.
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
 
var Const = BigInt.fromBaseString("2545F4914F6CDD1D", 16)
Line 1,267 ⟶ 1,528:
}
System.print("\nThe counts for 100,000 repetitions are:")
for (i in 0..4) System.print(" %(i) : %(counts[i])")</langsyntaxhighlight>
 
{{out}}
337

edits