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