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

From Rosetta Code
Content added Content deleted
No edit summary
Line 31: Line 31:
* Show your output here, on this page.
* Show your output here, on this page.



=={{header|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)-->
<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: #008080;">function</span> <span style="color: #000000;">random</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">seed</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">seed</span><span style="color: #0000FF;">*</span><span style="color: #000000;">seed</span><span style="color: #0000FF;">/</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1e6</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- seed = to_integer(sprintf("%012d",seed*seed)[4..9])</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">seed</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</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>
<!--</lang>-->


=={{header|Python}}==
=={{header|Python}}==

Revision as of 11:39, 2 January 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.


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.

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

Python

<lang 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())

</lang>

Output:
959861
333139
981593
524817
432883