Pseudo-random numbers/PCG32: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Minor tidy)
(Added FreeBASIC)
Line 899: Line 899:
H{ { 0 20049 } { 1 20022 } { 2 20115 } { 3 19809 } { 4 20005 } }
H{ { 0 20049 } { 1 20022 } { 2 20115 } { 3 19809 } { 4 20005 } }
</pre>
</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}}==
=={{header|Go}}==