Code Golf: MSM Random Number Generator
code-golf, language-agnostic, random, rosetta-stone
Solution
dc 26/37 chars
26 chars the function for a single number:
?dZsl2^dZ1+ll-2/Ar^/All^%p
37 chars with a 10 cycles loop:
?dZsl[2^dZ1+ll-2/Ar^/All^%pdzB>L]dsLx
Explanation of the function:
? Input n
dZ calculate number of digits
sl store in register l
2^ calculate n^2
dZ calculate number of digits of square
1+ll-2/Ar^/ n/(10^((squaredigits+1-l)/2)) int division truncates last digits
All^% n%(10^l) modulus truncates first digits
p print the number
Test:
dc msml.dc
45678
86479
78617
80632
1519
30736
47016
10504
3340
11556
35411
Problem
The challenge: The shortest code by character count that will generate a series of (pseudo)random numbers using the Middle-Square Method. The Middle-Square Method of (pseudo)random number generation was first suggested by John Von Neumann in 1946 and is defined as follows: Rn+1 = mid((Rn)2, m) For example: 34562 = 11943936 mid(11943936) = 9439 94392 = 89094721 mid(89094721) = 0947 9472 = 896809 mid(896809) = 9680 96802 = 93702400 mid(93702400) = 7024 Another example: 8432 = 710649 mid(710649) = 106 1062 = 11236 mid(11236) = 123 1232 = 15129 mid(15129) = 512 5122 = 262144 mid(262144) = 621 6212 = 385641 mid(385641) = 856 8562 = 732736 mid(732736) = 327 3272 = 106929 mid(106929) = 069 692 = 4761 mid(4761) = 476 4762 = 226576 mid(226576) = 265 Definition of `mid`: Apparently there is some confusion regarding the exact definition of `mid`. For the purposes of this challenge, assume that you are extracting the same number of digits as the starting seed. Meaning, if the starting seed had 4 digits, you would extract 4 digits from the middle. If the starting seed had 3 digits, you would extract 3 digits from the middle. Regarding the extraction of numbers when you can't find the exact middle, consider the number 710649. If you want to extract the middle 3, there is some ambiguity (106 or 064). In that case, extract the 3 that is closest to the beginning of the string. So in this case, you would extract 106. An easy way to think of it is to pad zeroes to the number if it's not the right number of digits. For example, if you pad leading-zeroes to 710649 you get 0710649 and the middle 3 digits now become 106. Test cases: Make no assumptions regarding the length of the seed. For example, you cannot assume that the seed will always be 4-digit number A starting seed of 3456 that generates 4-digit random-numbers should generate the following series (first 10): 9439, 947, 9680, 7024, 3365, 3232, 4458, 8737, 3351, 2292 A starting seed of 8653 that generates 4-digit random numbers should generate the following series (first 10): 8744, 4575, 9306, 6016, 1922, 6940, 1636, 6764, 7516, 4902 A starting seed of 843 that generates 3-digit random numbers should generate the following series (first 10): 106, 123, 512, 621, 856, 327, 69, 476, 265, 22 A starting seed of 45678 that generates 5-digit ranom numbers should generate the following series (first 10): 86479, 78617, 80632, 1519, 30736, 47016, 10504, 3340, 11556, 35411 As far as leading zeroes are concerned, the answer is no leading zeroes should be displayed :).