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

From Rosetta Code
Content added Content deleted
m (syntax highlighting fixup automation)
Line 33: Line 33:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
{{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 */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program pRandom64.s */
/* program pRandom64.s */
Line 113: Line 113:
/* for this file see task include a file in language AArch64 assembly */
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
~/.../rosetta/asm1 $ pRandom64
~/.../rosetta/asm1 $ pRandom64
Line 123: Line 123:
</pre>
</pre>
=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;


procedure Main is
procedure Main is
Line 138: Line 138:
end loop;
end loop;
New_Line;
New_Line;
end Main;</lang>
end Main;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 146: Line 146:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Uses (long) integers.
Uses (long) integers.
<lang algol68>BEGIN # generate random numbers by the middle-square method #
<syntaxhighlight lang="algol68">BEGIN # generate random numbers by the middle-square method #
INT seed := 675248;
INT seed := 675248;
# returns the next middle-square random number #
# returns the next middle-square random number #
Line 154: Line 154:
print( ( " ", whole( ms random, 0 ) ) )
print( ( " ", whole( ms random, 0 ) ) )
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 161: Line 161:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>on newGenerator(n, seed)
<syntaxhighlight lang="applescript">on newGenerator(n, seed)
script generator
script generator
property seed : missing value
property seed : missing value
Line 183: Line 183:
set end of output to generator's getRandom()
set end of output to generator's getRandom()
end repeat
end repeat
return output</lang>
return output</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>{959861, 333139, 981593, 524817, 432883}</lang>
<syntaxhighlight lang="applescript">{959861, 333139, 981593, 524817, 432883}</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
{{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 */
/* ARM assembly Raspberry PI or android with termux */
/* program pRandom.s */
/* program pRandom.s */
Line 315: Line 315:
/***************************************************/
/***************************************************/
.include "../affichage.inc"
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
959861
959861
Line 324: Line 324:
</pre>
</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PSEUDO-RANDOM_NUMBERS_MIDDLE-SQUARE_METHOD.AWK
# syntax: GAWK -f PSEUDO-RANDOM_NUMBERS_MIDDLE-SQUARE_METHOD.AWK
BEGIN {
BEGIN {
Line 342: Line 342:
return(seed)
return(seed)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 353: Line 353:


=={{header|C}}==
=={{header|C}}==
<lang c>#include<stdio.h>
<syntaxhighlight lang="c">#include<stdio.h>
long long seed;
long long seed;
long long random(){
long long random(){
Line 364: Line 364:
printf("%lld\n",random());
printf("%lld\n",random());
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
959861
959861
Line 373: Line 373:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <exception>
<syntaxhighlight lang="cpp">#include <exception>
#include <iostream>
#include <iostream>


Line 402: Line 402:
std::cout << msq.next() << std::endl;
std::cout << msq.next() << std::endl;
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>959861
<pre>959861
Line 411: Line 411:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>middle_square = cluster is seed, next
<syntaxhighlight lang="clu">middle_square = cluster is seed, next
rep = null
rep = null
own state: int
own state: int
Line 431: Line 431:
stream$putl(po, int$unparse(middle_square$next()))
stream$putl(po, int$unparse(middle_square$next()))
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>959861
<pre>959861
Line 440: Line 440:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. MIDDLE-SQUARE.
PROGRAM-ID. MIDDLE-SQUARE.
Line 464: Line 464:
MAKE-RANDOM.
MAKE-RANDOM.
MULTIPLY SEED BY SEED GIVING SQUARE.
MULTIPLY SEED BY SEED GIVING SQUARE.
MOVE NEXT-SEED TO SEED.</lang>
MOVE NEXT-SEED TO SEED.</syntaxhighlight>
{{out}}
{{out}}
<pre>959861
<pre>959861
Line 474: Line 474:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Pseudo-random numbers/Middle-square method. Nigel Galloway: January 5th., 2022
// 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")
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}}
{{out}}
<pre>
<pre>
Line 489: Line 489:
{{trans|Phix}}
{{trans|Phix}}
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: kernel math namespaces prettyprint ;
<syntaxhighlight lang="factor">USING: kernel math namespaces prettyprint ;


SYMBOL: seed
SYMBOL: seed
Line 496: Line 496:
: rand ( -- n ) seed get sq 1000 /i 1000000 mod dup seed set ;
: rand ( -- n ) seed get sq 1000 /i 1000000 mod dup seed set ;


5 [ rand . ] times</lang>
5 [ rand . ] times</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 509: Line 509:
{{works with|gforth|0.7.3}}
{{works with|gforth|0.7.3}}
The loop keeps the seed on top of the stack.
The loop keeps the seed on top of the stack.
<lang forth>: next-random dup * 1000 / 1000000 mod ;
<syntaxhighlight lang="forth">: next-random dup * 1000 / 1000000 mod ;
: 5-random-num 5 0 do next-random dup . loop ;
: 5-random-num 5 0 do next-random dup . loop ;
675248 5-random-num</lang>
675248 5-random-num</syntaxhighlight>


{{out}}
{{out}}
Line 518: Line 518:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>Dim Shared seed As Integer = 675248
<syntaxhighlight lang="freebasic">Dim Shared seed As Integer = 675248
Dim i As Integer
Dim i As Integer
Declare Function Rand As Integer
Declare Function Rand As Integer
Line 535: Line 535:
Rand = seed
Rand = seed
End Function
End Function
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 552: Line 552:
fmt.Println(seed)
fmt.Println(seed)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 564: Line 564:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>findPseudoRandom :: Int -> Int
<syntaxhighlight lang="haskell">findPseudoRandom :: Int -> Int
findPseudoRandom seed =
findPseudoRandom seed =
let square = seed * seed
let square = seed * seed
Line 572: Line 572:


solution :: [Int]
solution :: [Int]
solution = tail $ take 6 $ iterate findPseudoRandom 675248</lang>
solution = tail $ take 6 $ iterate findPseudoRandom 675248</syntaxhighlight>
{{out}}
{{out}}
<pre>[959861,333139,981593,524817,432883]</pre>
<pre>[959861,333139,981593,524817,432883]</pre>


=={{header|J}}==
=={{header|J}}==
<lang j>(_6{._3}.])&.:(10&#.^:_1)@(*~) ^: (>:i.6) 675248</lang>
<syntaxhighlight lang="j">(_6{._3}.])&.:(10&#.^:_1)@(*~) ^: (>:i.6) 675248</syntaxhighlight>
{{out}}
{{out}}
<pre>959861 333139 981593 524817 432883 387691</pre>
<pre>959861 333139 981593 524817 432883 387691</pre>
Line 585: Line 585:


The proposed PRNG hardly deserves the name and so this entry avoids it.
The proposed PRNG hardly deserves the name and so this entry avoids it.
<lang jq># Input: a positive integer
<syntaxhighlight lang="jq"># Input: a positive integer
# Output: the "middle-square"
# Output: the "middle-square"
def middle_square:
def middle_square:
Line 600: Line 600:
middle_square | ., middle_squares;
middle_square | ., middle_squares;


limit(5; 675248 | middle_squares)</lang>
limit(5; 675248 | middle_squares)</syntaxhighlight>
{{out}}
{{out}}
As expected.
As expected.
=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>const seed = [675248]
<syntaxhighlight lang="julia">const seed = [675248]


function random()
function random()
Line 617: Line 617:
println(random())
println(random())
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
959861
959861
Line 628: Line 628:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Raku}}
{{trans|Raku}}
<lang nim>proc rand:int =
<syntaxhighlight lang="nim">proc rand:int =
var seed {.global.} = 675248
var seed {.global.} = 675248
seed = int(seed*seed) div 1000 mod 1000000
seed = int(seed*seed) div 1000 mod 1000000
return seed
return seed


for _ in 1..5: echo rand()</lang>
for _ in 1..5: echo rand()</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 644: Line 644:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Pseudo-random_numbers/Middle-square_method
use strict; # https://rosettacode.org/wiki/Pseudo-random_numbers/Middle-square_method
Line 656: Line 656:
}
}


print msq, "\n" for 1 .. 5;</lang>
print msq, "\n" for 1 .. 5;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 668: Line 668:
=={{header|Phix}}==
=={{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.
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.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: Line 679:
<span style="color: #0000FF;">?</span><span style="color: #000000;">random</span><span style="color: #0000FF;">()</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure.i MSRandom()
<syntaxhighlight lang="purebasic">Procedure.i MSRandom()
Static.i seed=675248
Static.i seed=675248
seed = (seed*seed/1000)%1000000
seed = (seed*seed/1000)%1000000
Line 691: Line 691:
For i=1 To 5 : PrintN(Str(i)+": "+Str(MSRandom())) : Next
For i=1 To 5 : PrintN(Str(i)+": "+Str(MSRandom())) : Next
Input()
Input()
EndIf</lang>
EndIf</syntaxhighlight>
{{out}}
{{out}}
<pre>1: 959861
<pre>1: 959861
Line 700: Line 700:


=={{header|Python}}==
=={{header|Python}}==
<lang python>seed = 675248
<syntaxhighlight lang="python">seed = 675248
def random():
def random():
global seed
global seed
Line 710: Line 710:
for i in range(0,5):
for i in range(0,5):
print(random())
print(random())
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>959861
<pre>959861
Line 719: Line 719:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>sub msq {
<syntaxhighlight lang="raku" line>sub msq {
state $seed = 675248;
state $seed = 675248;
$seed = $seed² div 1000 mod 1000000;
$seed = $seed² div 1000 mod 1000000;
}
}


say msq() xx 5;</lang>
say msq() xx 5;</syntaxhighlight>
{{out}}
{{out}}
<pre>(959861 333139 981593 524817 432883)</pre>
<pre>(959861 333139 981593 524817 432883)</pre>
Line 730: Line 730:
=={{header|Red}}==
=={{header|Red}}==
{{trans|Phix}}
{{trans|Phix}}
<lang rebol>Red[]
<syntaxhighlight lang="rebol">Red[]
seed: 675248
seed: 675248
rand: does [seed: to-integer (seed * 1.0 * seed / 1000) % 1000000] ; multiply by 1.0 to avoid integer overflow (32-bit)
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]</lang>
loop 5 [print rand]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 743: Line 743:
</pre>
</pre>
=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def middle_square (seed)
<syntaxhighlight lang="ruby">def middle_square (seed)
return to_enum(__method__, seed) unless block_given?
return to_enum(__method__, seed) unless block_given?
s = seed.digits.size
s = seed.digits.size
Line 749: Line 749:
end
end


puts middle_square(675248).take(5)</lang>
puts middle_square(675248).take(5)</syntaxhighlight>
{{out}}
{{out}}
<pre>959861
<pre>959861
Line 758: Line 758:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>class MiddleSquareMethod(seed, k = 1000) {
<syntaxhighlight lang="ruby">class MiddleSquareMethod(seed, k = 1000) {
method next {
method next {
seed = (seed**2 // k % k**2)
seed = (seed**2 // k % k**2)
Line 765: Line 765:


var obj = MiddleSquareMethod(675248)
var obj = MiddleSquareMethod(675248)
say 5.of { obj.next }</lang>
say 5.of { obj.next }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 773: Line 773:
=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
{{trans|C}}
{{trans|C}}
<lang>If Info("wordsize") < 64 Then Print "This needs a 64-bit uBasic" : End
<syntaxhighlight lang="text">If Info("wordsize") < 64 Then Print "This needs a 64-bit uBasic" : End


s = 675248
s = 675248
Line 782: Line 782:
End
End


_random Param (1) : Return (a@*a@/1000%1000000)</lang>
_random Param (1) : Return (a@*a@/1000%1000000)</syntaxhighlight>
{{out}}
{{out}}
<pre>959861
<pre>959861
Line 795: Line 795:
=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|zsh}}
{{works with|zsh}}
<lang bash>seed=675248
<syntaxhighlight lang="bash">seed=675248
random(){
random(){
seed=`expr $seed \* $seed / 1000 % 1000000`
seed=`expr $seed \* $seed / 1000 % 1000000`
Line 804: Line 804:
random
random
echo $?
echo $?
done</lang>
done</syntaxhighlight>
{{out}}
{{out}}
The same as Python's
The same as Python's
Line 815: Line 815:
{{works with|Visual Basic|5}}
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
{{works with|Visual Basic|6}}
<lang vb>Option Explicit
<syntaxhighlight lang="vb">Option Explicit
Dim seed As Long
Dim seed As Long
Sub Main()
Sub Main()
Line 832: Line 832:
seed = Val(Mid(s, 4, 6))
seed = Val(Mid(s, 4, 6))
Rand = seed
Rand = seed
End Function</lang>
End Function</syntaxhighlight>
{{out}}
{{out}}
As expected.
As expected.


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var random = Fn.new { |seed| ((seed * seed)/1e3).floor % 1e6 }
<syntaxhighlight lang="ecmascript">var random = Fn.new { |seed| ((seed * seed)/1e3).floor % 1e6 }


var seed = 675248
var seed = 675248
for (i in 1..5) System.print(seed = random.call(seed))</lang>
for (i in 1..5) System.print(seed = random.call(seed))</syntaxhighlight>


{{out}}
{{out}}
Line 852: Line 852:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>real Seed;
<syntaxhighlight lang="xpl0">real Seed;
func Random;
func Random;
[Seed:= Floor(Mod(Seed*Seed/1e3, 1e6));
[Seed:= Floor(Mod(Seed*Seed/1e3, 1e6));
Line 862: Line 862:
for N:= 1 to 5 do
for N:= 1 to 5 do
[IntOut(0, Random); ChOut(0, ^ )];
[IntOut(0, Random); ChOut(0, ^ )];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}

Revision as of 11:30, 28 August 2022

Task
Pseudo-random numbers/Middle-square method
You are encouraged to solve this task according to the task description, using any language you may know.
Middle-square_method Generator
The Method

To generate a sequence of n-digit pseudorandom numbers, an n-digit starting value is created and squared, producing a 2n-digit number. If the result has fewer than 2n digits, leading zeroes are added to compensate. The middle n digits of the result would be the next number in the sequence and returned as the result. This process is then repeated to generate more numbers.

Pseudo code
var seed = 675248
function random()
    var s = str(seed * seed) 'str: turn a number into string
    do while not len(s) = 12
        s = "0" + s          'add zeroes before the string
    end do
    seed = val(mid(s, 4, 6)) 'mid: string variable, start, length
                             'val: turn a string into number
    return seed
end function
Middle-square method use
for i = 1 to 5
    print random()
end for
Task
  • Generate a class/set of functions that generates pseudo-random

numbers (6 digits) as shown above.

  • Show the first five integers generated with the seed 675248 as shown above.
  • Show your output here, on this page.

AArch64 Assembly

Works with: as version Raspberry Pi 3B version Buster 64 bits
or android 64 bits with application Termux
/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program pRandom64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
sMessResult:        .asciz " @ \n"
szCarriageReturn:   .asciz "\n"

qSeed:              .quad 675248
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sZoneConv:                  .skip 24
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                             // entry of program 

    ldr x0,qAdrqSeed
    ldr x3,[x0]
    mov x2,#5
1:
    mov x0,x3
    bl computePseudo
    mov x3,x0
    ldr x1,qAdrsZoneConv
    bl conversion10               // call décimal conversion
    ldr x0,qAdrsMessResult
    ldr x1,qAdrsZoneConv          // insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess              // display message
    subs x2,x2,#1
    bgt 1b
 
100:                              // standard end of the program 
    mov x0, #0                    // return code
    mov x8, #EXIT                 // request to exit program
    svc #0                        // perform the system call
qAdrszCarriageReturn:   .quad szCarriageReturn
qAdrsMessResult:        .quad sMessResult
qAdrsZoneConv:          .quad sZoneConv
qAdrqSeed:              .quad qSeed
/***************************************************/
/*   compute pseudo random number                  */
/***************************************************/
/* x0 contains the number            */
computePseudo:
    stp x1,lr,[sp,-16]! // save  registers
    stp x2,x3,[sp,-16]! // save  registers
    mov x2,x0
    mul x0,x2,x2
    ldr x2,qdiv
    udiv x1,x0,x2
    ldr x2,qdiv2
    udiv x0,x1,x2
    msub x0,x2,x0,x1
    ldp x2,x3,[sp],16   // restaur  2 registers
    ldp x1,lr,[sp],16   // restaur  2 registers
    ret                 // return to address lr x30
qdiv:            .quad 1000
qdiv2:           .quad 1000000

/********************************************************/
/*        File Include fonctions                        */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
~/.../rosetta/asm1 $ pRandom64
 959861
 333139
 981593
 524817
 432883

Ada

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   type long is range 0 .. 2**64;
   Seed : long := 675_248;
   function random return long is
   begin
      Seed := Seed * Seed / 1_000 rem 1_000_000;
      return Seed;
   end random;
begin
   for I in 1 .. 5 loop
      Put (long'Image (random));
   end loop;
   New_Line;
end Main;
Output:
 959861 333139 981593 524817 432883

ALGOL 68

Uses (long) integers.

BEGIN # generate random numbers by the middle-square method #
    INT seed := 675248;
    # returns the next middle-square random number #
    PROC ms random = INT: seed := SHORTEN( ( ( LONG INT( seed ) * LONG INT( seed ) ) OVER 1000 ) MOD 1 000 000 );
    # test the ms random procedure #
    FOR i TO 5 DO
        print( ( " ", whole( ms random, 0 ) ) )
    OD
END
Output:
 959861 333139 981593 524817 432883

AppleScript

on newGenerator(n, seed)
    script generator
        property seed : missing value
        property p1 : 10 ^ (n div 2)
        property p2 : 10 ^ n
        
        on getRandom()
            set seed to seed * seed div p1 mod p2
            return seed div 1
        end getRandom
    end script
    
    set generator's seed to seed mod (10 ^ n)
    return generator
end newGenerator

local generator, output
set generator to newGenerator(6, 675248)
set output to {}
repeat 5 times
    set end of output to generator's getRandom()
end repeat
return output
Output:
{959861, 333139, 981593, 524817, 432883}

ARM Assembly

Works with: as version Raspberry Pi
or android 32 bits with application Termux
/* ARM assembly Raspberry PI or android with termux */
/*  program pRandom.s   */
 
 /* REMARK 1 : this program use routines in a include file 
   see task Include a file language arm assembly 
   for the routine affichageMess conversion10 
   see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes                       */
/************************************/
.include "../constantes.inc"
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
sMessResult:        .asciz " @ \n"
szCarriageReturn:   .asciz "\n"

iSeed:              .int 675248
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sZoneConv:                  .skip 24
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                             @ entry of program 

    ldr r0,iAdriSeed
    ldr r3,[r0]
    mov r2,#5
1:
    mov r0,r3
    bl computePseudo
    mov r3,r0
    ldr r1,iAdrsZoneConv
    bl conversion10               @ call décimal conversion
    ldr r0,iAdrsMessResult
    ldr r1,iAdrsZoneConv          @ insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess              @ display message
    subs r2,r2,#1
    bgt 1b
 
100:                              @ standard end of the program 
    mov r0, #0                    @ return code
    mov r7, #EXIT                 @ request to exit program
    svc #0                        @ perform the system call
iAdrszCarriageReturn:   .int szCarriageReturn
iAdrsMessResult:        .int sMessResult
iAdrsZoneConv:          .int sZoneConv
iAdriSeed:              .int iSeed
/***************************************************/
/*   compute pseudo random number                  */
/***************************************************/
/* r0 contains the number            */
computePseudo:
    push {r1-r2,lr}           @ save registers 
    mov r2,r0
    umull r0,r1,r2,r2
    ldr r2,idiv
    bl division32R
    ldr r2,idiv2
    bl division32R
    mov r0,r2
    pop {r1-r2,pc}            @ restaur registers
idiv:            .int 1000
idiv2:           .int 1000000
/***************************************************/
/*   division number 64 bits in 2 registers by number 32 bits */
/***************************************************/
/* r0 contains lower part dividende   */
/* r1 contains upper part dividende   */
/* r2 contains divisor   */
/* r0 return lower part quotient    */
/* r1 return upper part quotient    */
/* r2 return remainder               */
division32R:
    push {r3-r9,lr}    @ save registers
    mov r6,#0          @ init upper upper part remainder  !!
    mov r7,r1          @ init upper part remainder with upper part dividende
    mov r8,r0          @ init lower part remainder with lower part dividende
    mov r9,#0          @ upper part quotient 
    mov r4,#0          @ lower part quotient
    mov r5,#32         @ bits number
1:                     @ begin loop
    lsl r6,#1          @ shift upper upper part remainder
    lsls r7,#1         @ shift upper  part remainder
    orrcs r6,#1        
    lsls r8,#1         @ shift lower  part remainder
    orrcs r7,#1
    lsls r4,#1         @ shift lower part quotient
    lsl r9,#1          @ shift upper part quotient
    orrcs r9,#1
                       @ divisor sustract  upper  part remainder
    subs r7,r2
    sbcs  r6,#0        @ and substract carry
    bmi 2f             @ négative ?
    
                       @ positive or equal
    orr r4,#1          @ 1 -> right bit quotient
    b 3f
2:                     @ negative 
    orr r4,#0          @ 0 -> right bit quotient
    adds r7,r2         @ and restaur remainder
    adc  r6,#0 
3:
    subs r5,#1         @ decrement bit size 
    bgt 1b             @ end ?
    mov r0,r4          @ lower part quotient
    mov r1,r9          @ upper part quotient
    mov r2,r7          @ remainder
100:                   @ function end
    pop {r3-r9,lr}     @ restaur registers
    bx lr  
/***************************************************/
/*      ROUTINES INCLUDE                           */
/***************************************************/
.include "../affichage.inc"
959861
 333139
 981593
 524817
 432883

AWK

# syntax: GAWK -f PSEUDO-RANDOM_NUMBERS_MIDDLE-SQUARE_METHOD.AWK
BEGIN {
    seed = 675248
    srand(seed)
    for (i=1; i<=5; i++) {
      printf("%2d: %s\n",i,main())
    }
    exit(0)
}
function main(  s) {
    s = seed ^ 2
    while (length(s) < 12) {
      s = "0" s
    }
    seed = substr(s,4,6)
    return(seed)
}
Output:
 1: 959861
 2: 333139
 3: 981593
 4: 524817
 5: 432883

C

#include<stdio.h>
long long seed;
long long random(){
        seed = seed * seed / 1000 % 1000000;
        return seed;
}
int main(){
        seed = 675248;
        for(int i=1;i<=5;i++)
                printf("%lld\n",random());
        return 0;
}
Output:

959861 333139 981593 524817 432883

C++

#include <exception>
#include <iostream>

using ulong = unsigned long;

class MiddleSquare {
private:
    ulong state;
    ulong div, mod;
public:
    MiddleSquare() = delete;
    MiddleSquare(ulong start, ulong length) {
        if (length % 2) throw std::invalid_argument("length must be even");
        div = mod = 1;
        for (ulong i=0; i<length/2; i++) div *= 10;
        for (ulong i=0; i<length; i++) mod *= 10;
        state = start % mod;
    }
    
    ulong next() { 
        return state = state * state / div % mod;
    }
};

int main() {
    MiddleSquare msq(675248, 6);
    for (int i=0; i<5; i++)
        std::cout << msq.next() << std::endl;
    return 0;
}
Output:
959861
333139
981593
524817
432883

CLU

middle_square = cluster is seed, next
    rep = null
    own state: int
    
    seed = proc (s: int)
        state := s
    end seed
    
    next = proc () returns (int)
        state := (state ** 2) / 1000 // 1000000
        return(state)
    end next
end middle_square

start_up = proc ()
    po: stream := stream$primary_output()
    middle_square$seed(675248)
    for i: int in int$from_to(1, 5) do
        stream$putl(po, int$unparse(middle_square$next()))
    end
end start_up
Output:
959861
333139
981593
524817
432883

COBOL

       IDENTIFICATION DIVISION.
       PROGRAM-ID. MIDDLE-SQUARE.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STATE.
          03 SEED         PIC 9(6) VALUE 675248.
          03 SQUARE       PIC 9(12).
          03 FILLER       REDEFINES SQUARE.
             05 FILLER    PIC 9(3).
             05 NEXT-SEED PIC 9(6).
             05 FILLER    PIC 9(3).
       
       PROCEDURE DIVISION.
       BEGIN.
           PERFORM SHOW-NUM 5 TIMES.
           STOP RUN.
       
       SHOW-NUM.
           PERFORM MAKE-RANDOM.
           DISPLAY SEED.
       
       MAKE-RANDOM.
           MULTIPLY SEED BY SEED GIVING SQUARE.
           MOVE NEXT-SEED TO SEED.
Output:
959861
333139
981593
524817
432883

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")
Output:
959861
333139
981593
524817
432883

Factor

Translation of: Phix
Works with: Factor version 0.99 2021-06-02
USING: kernel math namespaces prettyprint ;

SYMBOL: seed
675248 seed set-global

: rand ( -- n ) seed get sq 1000 /i 1000000 mod dup seed set ;

5 [ rand . ] times
Output:
959861
333139
981593
524817
432883

Forth

Works with: gforth version 0.7.3

The loop keeps the seed on top of the stack.

: next-random dup * 1000 / 1000000 mod ;
: 5-random-num 5 0 do next-random dup . loop ;
675248 5-random-num
Output:
959861 333139 981593 524817 432883  ok


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

Go

package main

import "fmt"

func random(seed int) int {
    return seed * seed / 1e3 % 1e6
}

func main() {
    seed := 675248
    for i := 1; i <= 5; i++ {
        seed = random(seed)
        fmt.Println(seed)
    }
}
Output:
959861
333139
981593
524817
432883

Haskell

findPseudoRandom :: Int -> Int
findPseudoRandom seed = 
   let square = seed * seed
       squarestr = show square
       enlarged = replicate ( 12 - length squarestr ) '0' ++ squarestr
   in read $ take 6 $ drop 3 enlarged

solution :: [Int]
solution = tail $ take 6 $ iterate findPseudoRandom 675248
Output:
[959861,333139,981593,524817,432883]

J

(_6{._3}.])&.:(10&#.^:_1)@(*~) ^: (>:i.6) 675248
Output:
959861 333139 981593 524817 432883 387691

jq

Works with gojq, the Go implementation of jq

The proposed PRNG hardly deserves the name and so this entry avoids it.

# Input: a positive integer
# Output: the "middle-square"
def middle_square:
  (tostring|length) as $len
  | (. * .)
  | tostring
  | (3*length/4|ceil) as $n
  | .[ -$n : $len-$n]
  | if length == 0 then 0 else tonumber end;

# Input: a positive integer
# Output: middle_square, applied recursively
def middle_squares:
  middle_square | ., middle_squares;

limit(5; 675248 | middle_squares)
Output:

As expected.

Julia

const seed = [675248]

function random()
    s = string(seed[] * seed[], pad=12) # turn a number into string, pad to 12 digits
    seed[] = parse(Int, s[begin+3:end-3]) # take middle of number string, parse to Int
    return seed[]
end

# Middle-square method use

for i = 1:5
    println(random())
end
Output:
959861
333139
981593
524817
432883

Nim

Translation of: Raku
proc rand:int = 
  var seed {.global.} = 675248
  seed = int(seed*seed) div 1000 mod 1000000
  return seed 

for _ in 1..5: echo rand()
Output:
959861
333139
981593
524817
432883

Perl

#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Pseudo-random_numbers/Middle-square_method
use warnings;

sub msq
  {
  use feature qw( state );
  state $seed = 675248;
  $seed = sprintf "%06d", $seed ** 2 / 1000 % 1e6;
  }

print msq, "\n" for 1 .. 5;
Output:
959861
333139
981593
524817
432883

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.

with javascript_semantics
integer seed = 675248
function random()
    seed = remainder(floor(seed*seed/1000),1e6)
--  seed = to_integer(sprintf("%012d",seed*seed)[4..9])
    return seed
end function
for i=1 to 5 do
    ?random()
end for

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
Output:
1: 959861
2: 333139
3: 981593
4: 524817
5: 432883

Python

seed = 675248
def random():
    global seed
    s = str(seed ** 2)
    while len(s) != 12:
        s = "0" + s
    seed = int(s[3:9])
    return seed
for i in range(0,5):
    print(random())
Output:
959861
333139
981593
524817
432883

Raku

sub msq {
    state $seed = 675248;
    $seed = $seed² div 1000 mod 1000000;
}

say msq() xx 5;
Output:
(959861 333139 981593 524817 432883)

Red

Translation of: Phix
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]
Output:
959861
333139
981593
524817
432883

Ruby

def middle_square (seed)
  return to_enum(__method__, seed) unless block_given?
  s = seed.digits.size
  loop { yield seed = (seed*seed).to_s.rjust(s*2, "0")[s/2, s].to_i }
end

puts middle_square(675248).take(5)
Output:
959861
333139
981593
524817
432883

Sidef

class MiddleSquareMethod(seed, k = 1000) {
    method next {
        seed = (seed**2 // k % k**2)
    }
}

var obj = MiddleSquareMethod(675248)
say 5.of { obj.next }
Output:
[959861, 333139, 981593, 524817, 432883]

uBasic/4tH

Translation of: C
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)
Output:
959861
333139
981593
524817
432883

0 OK, 0:140 

UNIX Shell

Works with: zsh
seed=675248
random(){
        seed=`expr $seed \* $seed / 1000 % 1000000`
        return seed
}
for ((i=1;i<=5;i++));
do
        random
        echo $?
done
Output:

The same as Python's

VBA

See Visual Basic

Visual Basic

Translation of: FreeBASIC
Works with: Visual Basic version 5
Works with: Visual Basic version 6
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
Output:

As expected.

Wren

var random = Fn.new { |seed| ((seed * seed)/1e3).floor % 1e6 }

var seed = 675248
for (i in 1..5) System.print(seed = random.call(seed))
Output:
959861
333139
981593
524817
432883

XPL0

real Seed;
func Random;
[Seed:= Floor(Mod(Seed*Seed/1e3, 1e6));
return fix(Seed);
];

int N;
[Seed:= 675248.;
for N:= 1 to 5 do
    [IntOut(0, Random);  ChOut(0, ^ )];
]
Output:
959861 333139 981593 524817 432883