Pseudo-random numbers/PCG32: Difference between revisions

 
(4 intermediate revisions by 3 users not shown)
Line 591:
3 : 19809
4 : 20005</pre>
 
=={{header|Dart}}==
{{trans|Python}}
<syntaxhighlight lang="Dart">
import 'dart:math';
 
class PCG32 {
BigInt fState = BigInt.zero;
BigInt fInc = BigInt.zero;
final BigInt mask64 = (BigInt.one << 64) - BigInt.one;
final BigInt mask32 = (BigInt.one << 32) - BigInt.one;
final BigInt k = BigInt.parse('6364136223846793005');
 
PCG32(BigInt seedState, BigInt seedSequence) {
seed(seedState, seedSequence);
}
 
PCG32.noSeed() {
fState = BigInt.zero;
fInc = BigInt.zero;
}
 
void seed(BigInt seedState, BigInt seedSequence) {
fState = BigInt.zero;
fInc = ((seedSequence << 1) | BigInt.one) & mask64;
nextInt();
fState += seedState;
nextInt();
}
 
BigInt nextInt() {
BigInt old = fState;
fState = ((old * k) + fInc) & mask64;
BigInt xorshifted = ( ((old >> 18) ^ old) >> 27) & mask32;
BigInt rot = (old >> 59) & mask32;
BigInt shifted = (xorshifted >> rot.toInt()) | (xorshifted << ((-rot) & BigInt.from(31)).toInt());
return shifted & mask32;
}
 
double nextFloat() {
return nextInt().toDouble() / (BigInt.one << 32).toDouble();
}
 
List<BigInt> nextIntRange(int size) {
List<BigInt> result = [];
for (int i = 0; i < size; i++) {
result.add(nextInt());
}
return result;
}
}
 
void main() {
var pcg32 = PCG32(BigInt.from(42), BigInt.from(54));
 
for (int i = 0; i < 5; i++) {
print(pcg32.nextInt().toString());
}
 
pcg32.seed(BigInt.from(987654321), BigInt.one);
 
var count = <int, int>{};
 
for (int i = 0; i < 100000; i++) {
int key = (pcg32.nextFloat() * 5).truncate();
count[key] = (count[key] ?? 0) + 1;
}
 
print('\nThe counts for 100,000 repetitions are:');
count.forEach((key, value) {
print('$key : $value');
});
}
</syntaxhighlight>
{{out}}
<pre>
2707161783
2068313097
3122475824
2211639955
3215226955
 
The counts for 100,000 repetitions are:
2 : 20115
3 : 19809
0 : 20049
4 : 20005
1 : 20022
 
</pre>
 
=={{header|Delphi}}==
Line 809 ⟶ 899:
H{ { 0 20049 } { 1 20022 } { 2 20115 } { 3 19809 } { 4 20005 } }
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Const As Ulongint mask64 = &HFFFFFFFFFFFFFFFF
Const As Ulongint mask32 = &HFFFFFFFF
Const As Ulongint cte = 6364136223846793005
 
Dim Shared As Ulongint state, inc
 
Function next_int() As Ulongint
' return random 32 bit unsigned int
Dim As Ulongint old = state
state = ((old * cte) + inc) And mask64
Dim As Ulongint xorshifted = (((old Shr 18) Xor old) Shr 27) And mask32
Dim As Ulongint rot = (old Shr 59) And mask32
Dim As Ulongint answer = (xorshifted Shr rot) Or (xorshifted Shl ((-rot) And 31))
answer And= mask32
Return answer
End Function
 
Function next_float() As Double
' return random float between 0 and 1
Return next_int() / (2 ^ 32)
End Function
 
Sub seed(seed_state As Ulongint, seed_sequence As Ulongint)
state = 0
inc = ((seed_sequence Shl 1) Or 1) And mask64
next_int()
state = (state + seed_state) And mask64
next_int()
End Sub
 
Dim As Integer i, hist(4)
 
seed(42, 54)
For i = 1 To 5
Print next_int()
Next i
 
Print !"\nThe counts for 100,000 repetitions are:"
seed(987654321, 1)
For i = 1 To 100000
hist(floor(next_float() * 5)) += 1
Next i
For i = 0 To 4
Print Using "hist(#) = #####"; i; hist(i)
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>2707161783
2068313097
3122475824
2211639955
3215226955
 
The counts for 100,000 repetitions are:
hist(0) = 20049
hist(1) = 20022
hist(2) = 20115
hist(3) = 19809
hist(4) = 20005</pre>
 
=={{header|Go}}==
Line 1,037 ⟶ 1,191:
3 : 19809
4 : 20005</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with gojq, the Go implementation of jq'''
 
The following uses some functions from the [[:Category:jq/bitwise.jq|"bitwise" module]].
If, for example, your jq does not support modules, you could insert the relevant definitions
therefrom in place of the "include" directive.
<syntaxhighlight lang=jq>
include "bitwise" {search: "."}; # see above
 
def Const: 6364136223846793005;
def Mask64: 18446744073709551615; # i.e. (1 | leftshift(64)) - 1
def Mask32: 4294967295; # i.e. (1 | leftshift(32)) - 1
 
# An initialization function if you do not wish to use seed/2
def rcg32:
{state: 9600629759793949339, # 0x853c49e6748fea9b
inc: 15726070495360670683 # 0xda3e39cb94b95bdb
};
 
# Input: {state, inc}
# Output: {state, inc, nextInt}
def nextInt:
.state as $old
| .state = bitwise_and($old * Const + .inc; Mask64)
| bitwise_and(( bitwise_xor($old | rightshift(18); $old) | rightshift(27)); Mask32) as $xorshifted
| bitwise_and($old|rightshift(59) ; Mask32) as $rot
| .nextInt = bitwise_and(
bitwise_or(
$xorshifted | rightshift($rot) ;
$xorshifted | leftshift( bitwise_and( 32 - $rot; 31) )) ;
Mask32) ;
 
def nextFloat:
nextInt
| .nextFloat = .nextInt / pow(2;32);
 
def seed($seedState; $seedSequence):
{state: 0,
inc: bitwise_and( bitwise_xor($seedSequence|leftshift(1); 1); Mask64)
}
| nextInt
| .state += $seedState
| nextInt;
 
def task1($n):
foreach range(0; $n) as $i (seed(42; 54); nextInt)
| .nextInt;
 
def task2($n):
reduce range(0; $n) as $i (seed(987654321; 1);
nextFloat
| .counts[((.nextFloat * 5)|floor)] += 1)
| "\nThe counts for \($n) repetitions are:",
(range(0; 5) as $i | "\($i) : \(.counts[$i])") ;
 
task1(5),
task2(100000)
</syntaxhighlight>
{{output}}
<pre>
2707161783
2068313097
3122475824
2211639955
3215226955
 
The counts for 100000 repetitions are:
0 : 20049
1 : 20022
2 : 20115
3 : 19809
4 : 20005
</pre>
 
=={{header|Julia}}==
Line 2,564 ⟶ 2,794:
{{libheader|Wren-big}}
As Wren doesn't have a 64-bit integer type, we use BigInt instead.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
 
var Const = BigInt.new("6364136223846793005")
2,507

edits