Pseudo-random numbers/Middle-square method: Difference between revisions
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
(→{{header|ALGOL 68}}: Show output on 1 line,like the Raku sample) |
||
Line 40: | Line 40: | ||
# test the ms random procedure # |
# test the ms random procedure # |
||
FOR i TO 5 DO |
FOR i TO 5 DO |
||
print( ( whole( ms random, 0 ) |
print( ( " ", whole( ms random, 0 ) ) ) |
||
OD |
OD |
||
END</lang> |
END</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
|||
Same as the Python sample. |
|||
959861 333139 981593 524817 432883 |
|||
</pre> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
Revision as of 13:33, 2 January 2022
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.
ALGOL 68
Uses (long) integers. <lang algol68>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</lang>
- Output:
959861 333139 981593 524817 432883
FreeBASIC
<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 </lang>
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
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
Raku
<lang perl6>sub msq {
state $seed = 675248; $seed = $seed² div 1000 mod 1000000;
}
say msq() xx 5;</lang>
- Output:
(959861 333139 981593 524817 432883)