Pseudo-random numbers/Middle-square method: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add Miranda) |
(Grouping BASIC dialects) |
||
Line 392: | Line 392: | ||
5: 432883 |
5: 432883 |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
|||
==={{header|FreeBASIC}}=== |
|||
<syntaxhighlight lang="freebasic">Dim Shared seed As Integer = 675248 |
|||
Dim i As Integer |
|||
Declare Function Rand As Integer |
|||
For i = 1 To 5 |
|||
Print Rand |
|||
Next i |
|||
Sleep |
|||
Function Rand As Integer |
|||
Dim s As String |
|||
s = Str(seed ^ 2) |
|||
Do While Len(s) <> 12 |
|||
s = "0" + s |
|||
Loop |
|||
seed = Val(Mid(s, 4, 6)) |
|||
Rand = seed |
|||
End Function |
|||
</syntaxhighlight> |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">Procedure.i MSRandom() |
|||
Static.i seed=675248 |
|||
seed = (seed*seed/1000)%1000000 |
|||
ProcedureReturn seed |
|||
EndProcedure |
|||
If OpenConsole() |
|||
For i=1 To 5 : PrintN(Str(i)+": "+Str(MSRandom())) : Next |
|||
Input() |
|||
EndIf</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1: 959861 |
|||
2: 333139 |
|||
3: 981593 |
|||
4: 524817 |
|||
5: 432883</pre> |
|||
==={{header|uBasic/4tH}}=== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang="text">If Info("wordsize") < 64 Then Print "This needs a 64-bit uBasic" : End |
|||
s = 675248 |
|||
For i = 1 To 5 |
|||
Print Set(s, FUNC(_random(s))) |
|||
Next |
|||
End |
|||
_random Param (1) : Return (a@*a@/1000%1000000)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>959861 |
|||
333139 |
|||
981593 |
|||
524817 |
|||
432883 |
|||
0 OK, 0:140 |
|||
</pre> |
|||
==={{header|Visual Basic}}=== |
|||
{{trans|FreeBASIC}} |
|||
{{works with|Visual Basic|5}} |
|||
{{works with|Visual Basic|6}} |
|||
<syntaxhighlight lang="vb">Option Explicit |
|||
Dim seed As Long |
|||
Sub Main() |
|||
Dim i As Integer |
|||
seed = 675248 |
|||
For i = 1 To 5 |
|||
Debug.Print Rand |
|||
Next i |
|||
End Sub |
|||
Function Rand() As Variant |
|||
Dim s As String |
|||
s = CStr(seed ^ 2) |
|||
Do While Len(s) <> 12 |
|||
s = "0" + s |
|||
Loop |
|||
seed = Val(Mid(s, 4, 6)) |
|||
Rand = seed |
|||
End Function</syntaxhighlight> |
|||
{{out}} |
|||
As expected. |
|||
=={{header|bc}}== |
=={{header|bc}}== |
||
Line 609: | Line 695: | ||
{{out}} |
{{out}} |
||
<pre>959861 333139 981593 524817 432883 ok</pre> |
<pre>959861 333139 981593 524817 432883 ok</pre> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">Dim Shared seed As Integer = 675248 |
|||
Dim i As Integer |
|||
Declare Function Rand As Integer |
|||
For i = 1 To 5 |
|||
Print Rand |
|||
Next i |
|||
Sleep |
|||
Function Rand As Integer |
|||
Dim s As String |
|||
s = Str(seed ^ 2) |
|||
Do While Len(s) <> 12 |
|||
s = "0" + s |
|||
Loop |
|||
seed = Val(Mid(s, 4, 6)) |
|||
Rand = seed |
|||
End Function |
|||
</syntaxhighlight> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 821: | Line 886: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</syntaxhighlight>--> |
<!--</syntaxhighlight>--> |
||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic">Procedure.i MSRandom() |
|||
Static.i seed=675248 |
|||
seed = (seed*seed/1000)%1000000 |
|||
ProcedureReturn seed |
|||
EndProcedure |
|||
If OpenConsole() |
|||
For i=1 To 5 : PrintN(Str(i)+": "+Str(MSRandom())) : Next |
|||
Input() |
|||
EndIf</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1: 959861 |
|||
2: 333139 |
|||
3: 981593 |
|||
4: 524817 |
|||
5: 432883</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 945: | Line 992: | ||
<pre> |
<pre> |
||
[959861, 333139, 981593, 524817, 432883] |
[959861, 333139, 981593, 524817, 432883] |
||
</pre> |
|||
=={{header|uBasic/4tH}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang="text">If Info("wordsize") < 64 Then Print "This needs a 64-bit uBasic" : End |
|||
s = 675248 |
|||
For i = 1 To 5 |
|||
Print Set(s, FUNC(_random(s))) |
|||
Next |
|||
End |
|||
_random Param (1) : Return (a@*a@/1000%1000000)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>959861 |
|||
333139 |
|||
981593 |
|||
524817 |
|||
432883 |
|||
0 OK, 0:140 |
|||
</pre> |
</pre> |
||
Line 986: | Line 1,011: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
:''See [[Pseudo-random numbers/Middle-square method#Visual Basic|Visual Basic]]'' |
:''See [[Pseudo-random numbers/Middle-square method#Visual Basic|Visual Basic]]'' |
||
=={{header|Visual Basic}}== |
|||
{{trans|FreeBASIC}} |
|||
{{works with|Visual Basic|5}} |
|||
{{works with|Visual Basic|6}} |
|||
<syntaxhighlight lang="vb">Option Explicit |
|||
Dim seed As Long |
|||
Sub Main() |
|||
Dim i As Integer |
|||
seed = 675248 |
|||
For i = 1 To 5 |
|||
Debug.Print Rand |
|||
Next i |
|||
End Sub |
|||
Function Rand() As Variant |
|||
Dim s As String |
|||
s = CStr(seed ^ 2) |
|||
Do While Len(s) <> 12 |
|||
s = "0" + s |
|||
Loop |
|||
seed = Val(Mid(s, 4, 6)) |
|||
Rand = seed |
|||
End Function</syntaxhighlight> |
|||
{{out}} |
|||
As expected. |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |