Pseudo-random numbers/Xorshift star: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|Haskell}}: added solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 87:
{{trans|Python}}
<
UInt64 state
Line 113:
L 100'000
hist[Int(random_gen.next_float() * 5)]++
print(hist)</
{{out}}
Line 127:
=={{header|Ada}}==
Raises Unseeded_Error exception if state is not initialized before generating a pseudo-random value.
<
with Ada.Text_IO; use Ada.Text_IO;
Line 179:
end Main;
</syntaxhighlight>
{{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.
<
# 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</
{{out}}
<pre>
Line 248:
=={{header|C}}==
<
#include <stdint.h>
#include <stdio.h>
Line 299:
return 0;
}</
{{out}}
<pre>3540625527
Line 315:
=={{header|C++}}==
{{trans|C}}
<
#include <cstdint>
#include <iostream>
Line 368:
return 0;
}</
{{out}}
<pre>3540625527
Line 384:
=={{header|D}}==
{{trans|C++}}
<
import std.stdio;
Line 433:
writeln(i, ": ", v);
}
}</
{{out}}
<pre>3540625527
Line 451:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Xorshift_star;
Line 523:
{$IFNDEF UNIX} Readln; {$ENDIF}
end.</
=={{header|F_Sharp|F#}}==
===The Functions===
<
// 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>
===The Tasks===
<
Xstar32 1234567UL|>Seq.take 5|>Seq.iter(printfn "%d")
</syntaxhighlight>
{{out}}
<pre>
Line 545:
3809424708
</pre>
<
XstarF 987654321UL|>Seq.take 100000|>Seq.countBy(fun n->int(n*5.0))|>Seq.iter(printf "%A");printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 554:
=={{header|Factor}}==
<
prettyprint sequences ;
Line 584:
987654321 >>state
100,000 [ dup next-float 5 * >integer ] replicate nip
histogram .</
{{out}}
<pre>
Line 597:
=={{header|Go}}==
{{trans|Python}}
<
import (
Line 641:
fmt.Printf(" %d : %d\n", i, counts[i])
}
}</
{{out}}
Line 663:
Implement given algorithm as an instance of <code>RandomGen</code> class.
<
import Data.Word
import System.Random
Line 683:
randoms' = unfoldr (pure . next)
toFloat n = fromIntegral n / (2^32 - 1)</
Direct usage of generator:
Line 712:
=={{header|Java}}==
{{trans|C++}}
<
private static final long MAGIC = Long.parseUnsignedLong("2545F4914F6CDD1D", 16);
private long state;
Line 758:
}
}
}</
{{out}}
<pre>3540625527
Line 774:
=={{header|Julia}}==
{{trans|Python}}
<
const CONST = 0x2545F4914F6CDD1D
Line 811:
testXorShiftStar()
</
<pre>
3540625527
Line 823:
=={{header|Kotlin}}==
{{trans|Java}}
<
class XorShiftStar {
Line 871:
println("${iv.index}: ${iv.value}")
}
}</
{{out}}
<pre>3540625527
Line 887:
=={{header|Lua}}==
{{trans|C}}
<
local g = {
magic = 0x2545F4914F6CDD1D,
Line 927:
for i,v in pairs(counts) do
print(i..': '..v)
end</
{{out}}
<pre>3540625527
Line 942:
=={{header|Nim}}==
<
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(", ")</
{{out}}
Line 990:
=={{header|Perl}}==
<
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;</
{{out}}
<pre>Seed: 1234567, first 5 values:
Line 1,046:
As per [[Pseudo-random_numbers/PCG32#Phix]], resorting to mpfr/gmp
{{libheader|Phix/mpfr}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,103:
=={{header|Python}}==
<
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)</
{{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"
has $!state;
Line 1,188:
say "\nSeed: default; first five Int values";
$rng = Xorshift-star.new;
.say for $rng.next-int xx 5;</
{{out}}
<pre>Seed: 1234567; first five Int values
Line 1,209:
=={{header|REXX}}==
<
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 $</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,280:
=={{header|Ruby}}==
Using Ruby 3.0 end-les method def:
<
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(": ") }</
{{out}}
<pre>
Line 1,322:
=={{header|Sidef}}==
{{trans|Perl}}
<
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 }</
{{out}}
<pre>
Line 1,364:
=={{header|Swift}}==
<
struct XorshiftStar {
Line 1,408:
}
print(counts)</
{{out}}
Line 1,423:
{{libheader|Wren-big}}
As Wren doesn't have a 64-bit integer type, we use BigInt instead.
<
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])")</
{{out}}
|