这篇文章没有干货,纯属划水摸鱼。请不要学习。
当你自己写的时候Matlab在程序中,可能会遇到各种奇妙的问题。这时,如果你在命令行的窗口里一脸蒙圈,无奈地敲出一个why,运行会怎样?
可能是这样
>> why The bald and not excessively bald and not excessively smart hamster obeyed a terrified and not excessively terrified hamster.
翻译成中文是:这只秃顶的仓鼠,不是太秃顶,也不是太聪明,听着一只惊恐而不是太恐慌的仓鼠。
再运行一次,输出可能会变成:为了愚弄这位又高又好又聪明的系统工程师。
>> why To 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'); end switch randi(10) case 1 a = special_case; case {2, 3, 4} a = phrase; otherwise a = sentence; end a(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_case switch 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从三种类型中随机选择另一种函数
function a = phrase switch 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()随机选择,列举如下:
主语
- 专有名词
- 主格代词
- 冠词 名词短语
谓语:
- 及物动词 宾语
- 不如物动词。
宾语:
- 宾格代词
- 介词 名词短语。
名词短语:
- 名词
- 形容词短语 名词短语
- 形容词短语 名词
形容词短语:
- 形容词
- 形容词短语 and 形容词短语
- 副词 形容词短语
介词短语
- 介词 冠词 名词短语
- 介词 专有名词
- 介词 宾格代词
专有名词:12个人名。
名词:6:数学家、程序员、系统管理员、工程师、仓鼠、儿童
主格代词:5:你,我,他,她,他们
宾格代词:4个,我,他,她,所有(all)
名词化动词:2,爱,赞成
副词:很,不太,不太过分(not excessively)
形容词:7个:高,秃,年轻,聪明,富,惊恐,好
冠词:the, some, a
介词:of,from
现代时态动词:愚弄,取悦,满足
及物动词:10个:威胁,告诉,询问,帮助,遵守。
不及物动词:6个,坚持这么做,建议这么做,告诉我这么做,想要这么做,知道这是个好主意,想用这种方式做。
当然,像我一样划水摸鱼的朋友可以自行修改代码。比如,randi()生成的是伪随机数,这样如果每次启动matlab后第一次运行why(而不运行其他函数),输出的结果是固定的。所以你可以修改随机数种子,当why无参数时设置rng('shuffle'),也就是根据当前时间初始化生成器。你还可以修改输出各种内容的概率,或者将输出变成你喜欢的中文语句。