Pseudo-random numbers/Xorshift star: Difference between revisions
Content added Content deleted
(Add Rust implementation) |
(Added FreeBASIC) |
||
Line 593: | Line 593: | ||
H{ { 0 20103 } { 1 19922 } { 2 19937 } { 3 20031 } { 4 20007 } } |
H{ { 0 20103 } { 1 19922 } { 2 19937 } { 3 20031 } { 4 20007 } } |
||
</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 = &H2545F4914F6CDD1D |
|||
Dim Shared As Ulongint state |
|||
Sub seed(num As Ulongint) |
|||
state = num And mask64 |
|||
End Sub |
|||
Function next_int() As Ulongint |
|||
Dim As Ulongint x = state |
|||
x = (x Xor (x Shr 12)) And mask64 |
|||
x = (x Xor (x Shl 25)) And mask64 |
|||
x = (x Xor (x Shr 27)) And mask64 |
|||
state = x |
|||
Dim As Ulongint answer = (((x * cte) And mask64) Shr 32) And mask32 |
|||
Return answer |
|||
End Function |
|||
Function next_float() As Double |
|||
Return next_int() / (2 ^ 32) |
|||
End Function |
|||
Dim As Integer i, hist(4) |
|||
seed(1234567) |
|||
For i = 1 To 5 |
|||
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>3540625527 |
|||
2750739987 |
|||
4037983143 |
|||
1993361440 |
|||
3809424708 |
|||
The counts for 100,000 repetitions are: |
|||
hist(0) = 20103 |
|||
hist(1) = 19922 |
|||
hist(2) = 19937 |
|||
hist(3) = 20031 |
|||
hist(4) = 20007</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |