Pseudo-random numbers/Splitmix64: Difference between revisions

Added FreeBASIC
(Added Swift implementation of task)
(Added FreeBASIC)
 
Line 516:
16408922859458223821
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Const As Ulongint mask64 = &HFFFFFFFFFFFFFFFF
Const As Ulongint C1 = &H9E3779B97F4A7C15
Const As Ulongint C2 = &HBF58476D1CE4E5B9
Const As Ulongint C3 = &H94D049BB133111EB
 
Dim Shared As Ulongint state
 
Sub seed(num As Ulongint)
state = num And mask64
End Sub
 
Function next_int() As Ulongint
' return random int between 0 and 2^64
Dim As Ulongint z = state
state += C1
z = (z Xor (z Shr 30)) * C2
z = (z Xor (z Shr 27)) * C3
Return z Xor (z Shr 31)
End Function
 
Function next_float() As Double
' return random float between 0 and 1
Return next_int() / (2 ^ 64)
End Function
 
Dim As Integer i, hist(4)
 
seed(1234567)
For i = 0 To 4
Print next_int()
Next i
 
Print !"\nThe counts for 100,000 repetitions are:"
seed(987654321)
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>15550326404697827096
6457827717110365317
3203168211198807973
9817491932198370423
4593380528125082431
 
The counts for 100,000 repetitions are:
hist(0) = 20027
hist(1) = 19893
hist(2) = 20072
hist(3) = 19978
hist(4) = 20030</pre>
 
=={{header|Go}}==
2,169

edits