本文没有干货,纯属划水摸鱼。请大家切勿学习
当你运行自己编写的Matlab程序的时候,可能会遇到各种各样奇葩的问题。这时候如果你一脸蒙圈而又无奈地在命令行窗口中敲出一个why,运行,会如何呢?
很可能是这样
>> whyThe bald and not excessively bald and not excessively smart hamster obeyed a terrified and not excessively terrified hamster.翻译成 中文 就是:“这只秃而又不是过于秃而又不是过于聪明的仓鼠,听从了一个惊恐的而又不是过于惊恐的仓鼠。”
再运行一次,输出可能会变成:“为了愚弄这个又高又好又聪明的系统工程师。“
>> whyTo fool the tall good and smart system manager.再运行一次,输出又变成:”The rich rich and tall and good system manager suggested it.“
为了搞清楚Matlab的工程师们究竟在搞什么恶作剧,我翻出来这个函数的源代码看一下。
下面是Matlab自带的函数代码why.m的前几行内容:
function why(n)%WHY Provides succinct answers to almost any question.% WHY, by itself, provides a random answer.% WHY(N) provides the N-th answer.% Please embellish or modify this function to suit your own tastes. % Copyright 1984-2014 The MathWorks, Inc. if nargin > 0 dflt = rng(n,'v5uniform');endswitch randi(10) case 1 a = special_case; case {2, 3, 4} a = phrase; otherwise a = sentence;enda(1) = upper(a(1));disp(a);if nargin > 0 rng(dflt);end可见,如果运行why函数,就会用rand(i)生成随机数,进而随机生成答案。如果带一个参数n, 就会以这个参数做随机数种子,生成答案(这样一个n会对应唯一的答案)。
输出的答案有三种 类 型:special_case(特定短语),phrase(短语),sentence(句子)。下面分别进行分析。
一、special_case又是一个函数,该函数从12个特定的句子中随机选择一个
function a = special_caseswitch randi(12) case 1 a = 'why not?'; case 2 a = 'don''t ask!'; case 3 a = 'it''s your karma.'; case 4 a = 'stupid question!'; case 5 a = 'how should I know?'; case 6 a = 'can you rephrase that?'; case 7 a = 'it should be obvious.'; case 8 a = 'the devil made me do it.'; case 9 a = 'the computer did it.'; case 10 a = 'the customer is always right.'; case 11 a = 'in the beginning, God created the heavens and the earth...'; case 12 a = 'don''t you have something better to do?';end二、phrase是又一个函数,从3个类型中随机选择一种
function a = phraseswitch randi(3) case 1 a = ['for the ' nouned_verb ' ' prepositional_phrase '.']; case 2 a = ['to ' present_verb ' ' object '.']; case 3 a = ['because ' sentence];end即:
1. for the+名词化动词+介词短语
2 to+现在时态动词+宾语
3 because+句子
(由于英文不太好懂,下面不再粘贴源代码,直接展示中文翻译。)
三、sentence则由”主语+谓语“构成。
--------------------------------------------------------------------------------------------------
语句中的各个部分,每个部分选用的单词都是用randi()随机选择的,列举如下:
主语
谓语:
宾语:
名词短语:
形容词短语:
介词短语
专有名词:12个人名。
名词:6个:数学家,程序员,系统管理员,工程师,仓鼠,小孩
主格代词:5个:你,我,他,她,他们
宾格代词:4个,我,他,她,所有(all)
名词化动词:2个,爱,赞成
副词:很,不是很,不过分(not excessively)
形容词:7个:高,秃,年轻,聪明,富,惊恐,好
冠词:the, some, a
介词:of,from
现代时态动词:愚弄,取悦,满足
及物动词:10个:威胁,告诉,询问,帮助,遵守。
不及物动词:6个,坚持这么做,建议这么做,告诉我这么做,想要这么做,知道这是个好主意,想用这种方式做。
当然,像我一样划水摸鱼的朋友可以自行修改代码。比如,randi()生成的是伪随机数,这样如果每次启动matlab后第一次运行why(而不运行其他函数),输出的结果是固定的。所以你可以修改随机数种子,当why无参数时设置rng('shuffle'),也就是根据当前时间初始化生成器。你还可以修改输出各种内容的概率,或者将输出变成你喜欢的中文语句。

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删