Pseudo-random numbers/Middle-square method: Difference between revisions
Pseudo-random numbers/Middle-square method (view source)
Revision as of 11:30, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 33:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program pRandom64.s */
Line 113:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
~/.../rosetta/asm1 $ pRandom64
Line 123:
</pre>
=={{header|Ada}}==
<
procedure Main is
Line 138:
end loop;
New_Line;
end Main;</
{{out}}
<pre>
Line 146:
=={{header|ALGOL 68}}==
Uses (long) integers.
<
INT seed := 675248;
# returns the next middle-square random number #
Line 154:
print( ( " ", whole( ms random, 0 ) ) )
OD
END</
{{out}}
<pre>
Line 161:
=={{header|AppleScript}}==
<
script generator
property seed : missing value
Line 183:
set end of output to generator's getRandom()
end repeat
return output</
{{output}}
<
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI or android with termux */
/* program pRandom.s */
Line 315:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
959861
Line 324:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PSEUDO-RANDOM_NUMBERS_MIDDLE-SQUARE_METHOD.AWK
BEGIN {
Line 342:
return(seed)
}
</syntaxhighlight>
{{out}}
<pre>
Line 353:
=={{header|C}}==
<
long long seed;
long long random(){
Line 364:
printf("%lld\n",random());
return 0;
}</
{{out}}
959861
Line 373:
=={{header|C++}}==
<
#include <iostream>
Line 402:
std::cout << msq.next() << std::endl;
return 0;
}</
{{out}}
<pre>959861
Line 411:
=={{header|CLU}}==
<
rep = null
own state: int
Line 431:
stream$putl(po, int$unparse(middle_square$next()))
end
end start_up</
{{out}}
<pre>959861
Line 440:
=={{header|COBOL}}==
<
PROGRAM-ID. MIDDLE-SQUARE.
Line 464:
MAKE-RANDOM.
MULTIPLY SEED BY SEED GIVING SQUARE.
MOVE NEXT-SEED TO SEED.</
{{out}}
<pre>959861
Line 474:
=={{header|F_Sharp|F#}}==
<
// Pseudo-random numbers/Middle-square method. Nigel Galloway: January 5th., 2022
Seq.unfold(fun n->let n=n*n%1000000000L/1000L in Some(n,n)) 675248L|>Seq.take 5|>Seq.iter(printfn "%d")
</syntaxhighlight>
{{out}}
<pre>
Line 489:
{{trans|Phix}}
{{works with|Factor|0.99 2021-06-02}}
<
SYMBOL: seed
Line 496:
: rand ( -- n ) seed get sq 1000 /i 1000000 mod dup seed set ;
5 [ rand . ] times</
{{out}}
<pre>
Line 509:
{{works with|gforth|0.7.3}}
The loop keeps the seed on top of the stack.
<
: 5-random-num 5 0 do next-random dup . loop ;
675248 5-random-num</
{{out}}
Line 518:
=={{header|FreeBASIC}}==
<
Dim i As Integer
Declare Function Rand As Integer
Line 535:
Rand = seed
End Function
</syntaxhighlight>
=={{header|Go}}==
<
import "fmt"
Line 552:
fmt.Println(seed)
}
}</
{{out}}
Line 564:
=={{header|Haskell}}==
<
findPseudoRandom seed =
let square = seed * seed
Line 572:
solution :: [Int]
solution = tail $ take 6 $ iterate findPseudoRandom 675248</
{{out}}
<pre>[959861,333139,981593,524817,432883]</pre>
=={{header|J}}==
<
{{out}}
<pre>959861 333139 981593 524817 432883 387691</pre>
Line 585:
The proposed PRNG hardly deserves the name and so this entry avoids it.
<
# Output: the "middle-square"
def middle_square:
Line 600:
middle_square | ., middle_squares;
limit(5; 675248 | middle_squares)</
{{out}}
As expected.
=={{header|Julia}}==
<
function random()
Line 617:
println(random())
end
</
<pre>
959861
Line 628:
=={{header|Nim}}==
{{trans|Raku}}
<
var seed {.global.} = 675248
seed = int(seed*seed) div 1000 mod 1000000
return seed
for _ in 1..5: echo rand()</
{{out}}
<pre>
Line 644:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Pseudo-random_numbers/Middle-square_method
Line 656:
}
print msq, "\n" for 1 .. 5;</
{{out}}
<pre>
Line 668:
=={{header|Phix}}==
You don't actually have to use strings, but should you so desire the commented-out line gives exactly the same results. Output matches Python.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">seed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">675248</span>
Line 679:
<span style="color: #0000FF;">?</span><span style="color: #000000;">random</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
=={{header|PureBasic}}==
<
Static.i seed=675248
seed = (seed*seed/1000)%1000000
Line 691:
For i=1 To 5 : PrintN(Str(i)+": "+Str(MSRandom())) : Next
Input()
EndIf</
{{out}}
<pre>1: 959861
Line 700:
=={{header|Python}}==
<
def random():
global seed
Line 710:
for i in range(0,5):
print(random())
</syntaxhighlight>
{{out}}
<pre>959861
Line 719:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
state $seed = 675248;
$seed = $seed² div 1000 mod 1000000;
}
say msq() xx 5;</
{{out}}
<pre>(959861 333139 981593 524817 432883)</pre>
Line 730:
=={{header|Red}}==
{{trans|Phix}}
<
seed: 675248
rand: does [seed: to-integer (seed * 1.0 * seed / 1000) % 1000000] ; multiply by 1.0 to avoid integer overflow (32-bit)
loop 5 [print rand]</
{{out}}
<pre>
Line 743:
</pre>
=={{header|Ruby}}==
<
return to_enum(__method__, seed) unless block_given?
s = seed.digits.size
Line 749:
end
puts middle_square(675248).take(5)</
{{out}}
<pre>959861
Line 758:
=={{header|Sidef}}==
<
method next {
seed = (seed**2 // k % k**2)
Line 765:
var obj = MiddleSquareMethod(675248)
say 5.of { obj.next }</
{{out}}
<pre>
Line 773:
=={{header|uBasic/4tH}}==
{{trans|C}}
<syntaxhighlight lang="text">If Info("wordsize") < 64 Then Print "This needs a 64-bit uBasic" : End
s = 675248
Line 782:
End
_random Param (1) : Return (a@*a@/1000%1000000)</
{{out}}
<pre>959861
Line 795:
=={{header|UNIX Shell}}==
{{works with|zsh}}
<
random(){
seed=`expr $seed \* $seed / 1000 % 1000000`
Line 804:
random
echo $?
done</
{{out}}
The same as Python's
Line 815:
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
<
Dim seed As Long
Sub Main()
Line 832:
seed = Val(Mid(s, 4, 6))
Rand = seed
End Function</
{{out}}
As expected.
=={{header|Wren}}==
<
var seed = 675248
for (i in 1..5) System.print(seed = random.call(seed))</
{{out}}
Line 852:
=={{header|XPL0}}==
<
func Random;
[Seed:= Floor(Mod(Seed*Seed/1e3, 1e6));
Line 862:
for N:= 1 to 5 do
[IntOut(0, Random); ChOut(0, ^ )];
]</
{{out}}
|