Pseudo-random numbers/Middle-square method: Difference between revisions

m
syntax highlighting fixup automation
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">
<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>
</lang>
<pre>
~/.../rosetta/asm1 $ pRandom64
Line 123:
</pre>
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Main is
Line 138:
end loop;
New_Line;
end Main;</langsyntaxhighlight>
{{out}}
<pre>
Line 146:
=={{header|ALGOL 68}}==
Uses (long) integers.
<langsyntaxhighlight lang="algol68">BEGIN # generate random numbers by the middle-square method #
INT seed := 675248;
# returns the next middle-square random number #
Line 154:
print( ( " ", whole( ms random, 0 ) ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 161:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on newGenerator(n, seed)
script generator
property seed : missing value
Line 183:
set end of output to generator's getRandom()
end repeat
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{959861, 333139, 981593, 524817, 432883}</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI or android with termux */
/* program pRandom.s */
Line 315:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
959861
Line 324:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PSEUDO-RANDOM_NUMBERS_MIDDLE-SQUARE_METHOD.AWK
BEGIN {
Line 342:
return(seed)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 353:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include<stdio.h>
long long seed;
long long random(){
Line 364:
printf("%lld\n",random());
return 0;
}</langsyntaxhighlight>
{{out}}
959861
Line 373:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <exception>
#include <iostream>
 
Line 402:
std::cout << msq.next() << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>959861
Line 411:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">middle_square = cluster is seed, next
rep = null
own state: int
Line 431:
stream$putl(po, int$unparse(middle_square$next()))
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>959861
Line 440:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. MIDDLE-SQUARE.
Line 464:
MAKE-RANDOM.
MULTIPLY SEED BY SEED GIVING SQUARE.
MOVE NEXT-SEED TO SEED.</langsyntaxhighlight>
{{out}}
<pre>959861
Line 474:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 489:
{{trans|Phix}}
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: kernel math namespaces prettyprint ;
 
SYMBOL: seed
Line 496:
: rand ( -- n ) seed get sq 1000 /i 1000000 mod dup seed set ;
 
5 [ rand . ] times</langsyntaxhighlight>
{{out}}
<pre>
Line 509:
{{works with|gforth|0.7.3}}
The loop keeps the seed on top of the stack.
<langsyntaxhighlight lang="forth">: next-random dup * 1000 / 1000000 mod ;
: 5-random-num 5 0 do next-random dup . loop ;
675248 5-random-num</langsyntaxhighlight>
 
{{out}}
Line 518:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Dim Shared seed As Integer = 675248
Dim i As Integer
Declare Function Rand As Integer
Line 535:
Rand = seed
End Function
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 552:
fmt.Println(seed)
}
}</langsyntaxhighlight>
 
{{out}}
Line 564:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">findPseudoRandom :: Int -> Int
findPseudoRandom seed =
let square = seed * seed
Line 572:
 
solution :: [Int]
solution = tail $ take 6 $ iterate findPseudoRandom 675248</langsyntaxhighlight>
{{out}}
<pre>[959861,333139,981593,524817,432883]</pre>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">(_6{._3}.])&.:(10&#.^:_1)@(*~) ^: (>:i.6) 675248</langsyntaxhighlight>
{{out}}
<pre>959861 333139 981593 524817 432883 387691</pre>
Line 585:
 
The proposed PRNG hardly deserves the name and so this entry avoids it.
<langsyntaxhighlight lang="jq"># Input: a positive integer
# Output: the "middle-square"
def middle_square:
Line 600:
middle_square | ., middle_squares;
 
limit(5; 675248 | middle_squares)</langsyntaxhighlight>
{{out}}
As expected.
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const seed = [675248]
 
function random()
Line 617:
println(random())
end
</langsyntaxhighlight>{{out}}
<pre>
959861
Line 628:
=={{header|Nim}}==
{{trans|Raku}}
<langsyntaxhighlight lang="nim">proc rand:int =
var seed {.global.} = 675248
seed = int(seed*seed) div 1000 mod 1000000
return seed
 
for _ in 1..5: echo rand()</langsyntaxhighlight>
{{out}}
<pre>
Line 644:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Pseudo-random_numbers/Middle-square_method
Line 656:
}
 
print msq, "\n" for 1 .. 5;</langsyntaxhighlight>
{{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.i MSRandom()
Static.i seed=675248
seed = (seed*seed/1000)%1000000
Line 691:
For i=1 To 5 : PrintN(Str(i)+": "+Str(MSRandom())) : Next
Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre>1: 959861
Line 700:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">seed = 675248
def random():
global seed
Line 710:
for i in range(0,5):
print(random())
</syntaxhighlight>
</lang>
{{out}}
<pre>959861
Line 719:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub msq {
state $seed = 675248;
$seed = $seed² div 1000 mod 1000000;
}
 
say msq() xx 5;</langsyntaxhighlight>
{{out}}
<pre>(959861 333139 981593 524817 432883)</pre>
Line 730:
=={{header|Red}}==
{{trans|Phix}}
<langsyntaxhighlight lang="rebol">Red[]
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]</langsyntaxhighlight>
{{out}}
<pre>
Line 743:
</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def middle_square (seed)
return to_enum(__method__, seed) unless block_given?
s = seed.digits.size
Line 749:
end
 
puts middle_square(675248).take(5)</langsyntaxhighlight>
{{out}}
<pre>959861
Line 758:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">class MiddleSquareMethod(seed, k = 1000) {
method next {
seed = (seed**2 // k % k**2)
Line 765:
 
var obj = MiddleSquareMethod(675248)
say 5.of { obj.next }</langsyntaxhighlight>
{{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)</langsyntaxhighlight>
{{out}}
<pre>959861
Line 795:
=={{header|UNIX Shell}}==
{{works with|zsh}}
<langsyntaxhighlight lang="bash">seed=675248
random(){
seed=`expr $seed \* $seed / 1000 % 1000000`
Line 804:
random
echo $?
done</langsyntaxhighlight>
{{out}}
The same as Python's
Line 815:
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
<langsyntaxhighlight lang="vb">Option Explicit
Dim seed As Long
Sub Main()
Line 832:
seed = Val(Mid(s, 4, 6))
Rand = seed
End Function</langsyntaxhighlight>
{{out}}
As expected.
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var random = Fn.new { |seed| ((seed * seed)/1e3).floor % 1e6 }
 
var seed = 675248
for (i in 1..5) System.print(seed = random.call(seed))</langsyntaxhighlight>
 
{{out}}
Line 852:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">real Seed;
func Random;
[Seed:= Floor(Mod(Seed*Seed/1e3, 1e6));
Line 862:
for N:= 1 to 5 do
[IntOut(0, Random); ChOut(0, ^ )];
]</langsyntaxhighlight>
 
{{out}}
10,327

edits