Code Golf:MSM 随机数生成器
挑战:
按字符数计算的最短代码将使用 中方法。
(伪)随机数生成的中方法由 John Von Neumann 于 1946 年首次提出,定义如下:
Rn+1 = mid ((Rn)2, m)
例如:
34562 = 11943936
中(11943936) = 9439
94392 = 89094721
中(89094721) = 0947
9472 = 896809
中(896809) = 9680
96802 = 93702400
中(93702400) = 7024
另一个例子:
8432 = 710649
中(710649)= 106
1062 = 11236
中(11236)= 123
1232 = 15129
中(15129) = 512
5122 = 262144
中(262144) = 621
6212 = 385641
中(385641)= 856
8562 = 732736
中(732736) = 327
3272 = 106929
中(106929) = 069
692 = 4761
中(4761)= 476
4762 = 226576
中(226576) = 265
mid
的定义:
显然对于 mid
的确切定义存在一些混乱。出于本次挑战的目的,假设您正在提取与起始种子相同数量的数字。这意味着,如果起始种子有 4 位数字,您将从中间提取 4 位数字。如果起始种子有 3 位数字,您将从中间提取 3 位数字。
关于在无法找到精确中间位置时提取数字的问题,请考虑数字710649。如果要提取中间的 3,则存在一些歧义(106 或 064)。在这种情况下,提取最接近字符串开头的 3。因此,在本例中,您将提取 106。
一种简单的思考方法是,如果数字位数不正确,则在数字中填充零。例如,如果将前导零填充到 710649,则会得到 0710649,中间的 3 位数字现在变为 106。
测试用例:
不对种子的长度做出任何假设。例如,您不能假设种子始终是 4 位数字
生成 4 位随机数的起始种子 3456 应生成以下序列(前 10 个):
9439、947、9680、7024、3365、3232、4458、8737、3351、2292
生成 4 位随机数的起始种子 8653 应生成以下序列(前 10 个):
8744、4575、9306、6016、1922、6940、1636、6764、7516、4902
生成 3 位随机数的 843 起始种子应生成以下序列(前 10 个):
106、123、512、621、856、327、69、476、265、22
生成 5 位随机数的起始种子 45678 应生成以下序列(前 10 个):
86479、78617、80632、1519、30736、47016、10504、3340、11556、35411
就前导零而言,答案是不应显示前导零:)。
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 :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
Google 文档 - 电子表格:42 个字符
用法:
C2
中,然后拖动所有序列的公式。测试用例:
屏幕截图:
Code Golf:MSM 随机数生成器 http://img59.imageshack.us/ img59/6830/golfo.png
限制:
INT()
来修剪它们。Google Docs - Spreadsheet: 42 chars
Usage:
C2
, and drag the formula for all the sequence.Test Cases:
Screenshot:
Code Golf: MSM Random Number Generator http://img59.imageshack.us/img59/6830/golfo.png
Limitations:
INT()
on the results.dc 26/37 chars
26 chars 单个数字的函数:
37 chars 10 个循环循环:
函数说明:
测试:
dc 26/37 chars
26 chars the function for a single number:
37 chars with a 10 cycles loop:
Explanation of the function:
Test:
Python(86 个字符)
在标准输出上生成无限序列。请注意,反引号技巧至少在
long
类型的旧版本上不起作用,因为表示形式中以L
结尾。 Python 3print
作为函数会添加 1 个以上的字符来结束括号。Python (86 chars)
Produces infinite sequence on stdout. Note that backtick trick won't work at least on older versions with
long
type because of the endingL
in representation. Python 3print
as function would add 1 more char for closing paren.C#(
9681798584 个字符)更改日志
我的代码:
使用示例:
结果
C# (
9681798584 chars)Change log
My Code:
Usage Example:
Results
Perl (
1029695939279 个字符)$s =$_;$d=长度$s;map{$s*=$s;print 0+($s=substr$s,($s=~y///c-$d)/2,$d ),$/}0..9
Perl (
1029695939279 chars)$s=$_;$d=length$s;map{$s*=$s;print 0+($s=substr$s,($s=~y///c-$d)/2,$d),$/}0..9
Groovy -
使用第一个参数作为种子的 82 个字符,并由 4 个 base10 数字组成的输出,如您的示例中所示。
Groovy - 82 chars
using first argument as seed and output made by 4 base10 digits like in your examples..
Perl,80 个字符
(来自命令行)
Perl, 80 chars
(from commandline)
Ruby,
857669 个字符(生成并打印 10 个数字)从标准输入读取。
输出
Ruby,
857669 chars (generates and prints 10 numbers)Reads from standard input.
Output
JavaScript:91 个字符
用法:
完整测试用例:
测试结果:
JavaScript: 91 characters
Usage:
Full Test Cases:
Test Results:
Haskell
注意:生成一个包含 MSM 随机数的无限列表。
函数,81
用法示例:
输出示例:
程序,103
Haskell
Note: An infinite list is produced containing MSM random numbers.
Function, 81
Example usage:
Example output:
Program, 103
Haskell (97
99chars)可能仍然可以缩短。产生一个无限序列,或者至少遇到 0,在这种情况下它会崩溃。
用法:
b <数字长度>
解释
该程序不是应用子字符串并使用字符串长度算术,而是在删除最后一个字符 (
init
) 和删除第一个字符之间进行迭代(tail
) 直到达到所需的长度。正如迭代以及大多数 Haskell 函数一样,假设所使用的函数是常量。由于函数本身发生了变化,因此我们需要实现一个特殊版本的迭代。
Haskell (97
99chars)Can probably still be shortened. Produces an infinite sequence, or at least it encounters 0, in which case it crashes.
Usage:
b <length of number> <number>
Explanation
Rather than applying substring and using string length arithmetic, this program iterates between removing the last character (
init
), and removing the first character (tail
) until the desired length is reached.As
iterate
as well as most Haskell functions assume that the function used is constant. Since we have the function itself changing, we need to implement a special version ofiterate
.Ruby(66 个字符)
假设整数输入:
Ruby (66 chars)
Assuming integer inputs:
Lua (
135128114 个字符)带有一个参数的种子并返回第一个 MSM 随机整数;不带参数的后续调用将返回下一个 MSM 随机整数。调用另一个整数来重新播种。
测试:
Lua (
135128114 chars)Seeds with one argument and returns first MSM random integer; subsequent calls with no arguments return next MSM random integer. Call will another integer to re-seed.
Test:
Perl -
112 - 现在,108- 现在 95 (感谢 Zaid 的想法) - 字符没有空格,不包括测试驱动程序循环(例如,我只计算了生成 1 的代码) sequence) - foreach 循环体中的代码。输出:
压缩后的代码为 112:
Perl -
112 - now, 108- now 95 (thanks to Zaid's idea) - chars sans white space, excluding test driver loop (e.g. I only counted the code to generate 1 sequence) - the code in the body of foreach loop.Output:
Compressed code that was 112:
Perl,
1009492919088 个字符通过标准输入提供的种子。为了可读性而包含换行符:
Perl,
1009492919088 charsSeed provided through standard input. Newlines included for readability: