Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 用实词生成随机URL_Ruby_Url_Random - Fatal编程技术网

Ruby 用实词生成随机URL

Ruby 用实词生成随机URL,ruby,url,random,Ruby,Url,Random,我已经写了一个小脚本,将生成一个随机的URL,它的工作,但我希望生成的URL是有点可信的,这意味着我希望它生成真实的话。到目前为止,它生成7个随机字符和数字 def generate_url(length=7) protocall = %w(https:// http://) body = rand(36**length).to_s(36) num = rand(1..999).to_s url = "#{protocall.sample.to_s}#{body}.com/php

我已经写了一个小脚本,将生成一个随机的URL,它的工作,但我希望生成的URL是有点可信的,这意味着我希望它生成真实的话。到目前为止,它生成7个随机字符和数字

def generate_url(length=7)
  protocall = %w(https:// http://)
  body = rand(36**length).to_s(36)
  num = rand(1..999).to_s
  url = "#{protocall.sample.to_s}#{body}.com/php?id=#{num}"
  puts url
end

#<= http://s857yi5.com/php?id=168
#<= https://6rm0oq3.com/php?id=106
#<= http://skhvk1n.com/php?id=306
def生成url(长度=7)
protocall=%w(https://http://)
主体=兰特(36**长度)。至(36)
num=rand(1..999)。至
url=“#{protocall.sample.to#s}{body}.com/php?id=#{num}”
放置url
结束

# 在unix系统中,可以使用字典中的任意数量的单词。您通常可以在路径/usr/share/dict/words中找到它

免责声明:此答案面向在
unix
系统中寻求此问题解决方案的开发人员。然而,事实并非如此 针对非unix系统解决此问题


您可以使用
ruby
系统调用来执行此操作。Unix系统内置有从文件中随机获取行的命令

好消息是,unix系统在
usr/share/dict/words
上也有完整的英语词典。所以,在ruby中我会这样做

`shuf -n 1 /usr/share/dict/words`.chomp
=> "dastardly"
注意:这里我使用了
backtick
作为
system
调用。和

所以URL应该是

random_word = `shuf -n 1 /usr/share/dict/words`.chomp
url = "#{random_word}#{body}.com/php?id=#{num}"
=> "wrongfullythisis_body_part.com/php?id=123"

通过交替使用元音和辅音,您可以生成可发音但无意义的单词:tifa zakohu ayanipico wis kicevepys ijoxar uhiq ilay og luh tanise rijux tejod kuyasoq zov wu

试试faker是生成单词、电子邮件、URL或任何您需要的东西的酷宝石

我在很多项目上都用过

hb@hora ~ » irb
2.2.3 :001 > require 'faker'
 => true 
2.2.3 :002 > Faker::Lorem.sentence(3)
 => "Ea esse ex." 
2.2.3 :003 > Faker::Lorem.sentence(3)
 => "Fugiat odio harum." 
2.2.3 :004 > Faker::Lorem.words
 => ["consequuntur", "labore", "optio"] 
2.2.3 :005 > Faker::Lorem.word
 => "error" 
2.2.3 :006 > 
但是如果您不能添加外部gem,您可以创建自己的数组/字典

2.2.3 :013 > dict
 => ["Editors", "and", "critics", "of", "the", "plays", "disdaining", "the", "showiness", "and", "melodrama", "of", "Shakespearean", "stage", "representation", "began", "to", "focus", "on", "Shakespeare", "as", "a", "dramatic", "poet", "to", "be", "studied", "on", "the", "printed", "page", "rather", "than", "in", "the", "theatre", "The", "rift", "between", "Shakespeare", "on", "the", "stage", "and", "Shakespeare", "on", "the", "page", "was", "at", "its", "widest", "in", "the", "early", "19th", "century", "at", "a", "time", "when", "both", "forms", "of", "Shakespeare", "were", "hitting", "peaks", "of", "fame", "and", "popularity", "theatrical"] 
2.2.3 :014 > dict.sample
 => "the" 
2.2.3 :015 > dict.sample
 => "a" 
2.2.3 :016 > dict.sample
 => "disdaining" 
2.2.3 :017 > dict.sample
 => "century" 
2.2.3 :018 > 
该词典是通过将维基百科的文本复制粘贴到我自己的irb,然后扫描全部/w而创建的+/

2.2.3 :023 > dict='n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language.'
 => "n his own time, William Shakespeare (1564–1616) was rated as merely one among many talented playwrights and poets, but since the late 17th century he has been considered the supreme playwright and poet of the English language." 
2.2.3 :024 > dict.scan(/\w+/)
 => ["n", "his", "own", "time", "William", "Shakespeare", "1564", "1616", "was", "rated", "as", "merely", "one", "among", "many", "talented", "playwrights", "and", "poets", "but", "since", "the", "late", "17th", "century", "he", "has", "been", "considered", "the", "supreme", "playwright", "and", "poet", "of", "the", "English", "language"]

我找到了另一种使用字典列表的方法,如果您使用Windows并有权访问Outlook,则可以使用Outlook
default.DIC
文件作为单词列表,这将为您提供大量单词,您所要做的就是将其复制到程序中。参考是

您可以提供一个简短单词列表,并将其中几个组合成所需的字符串。如果没有真实单词列表,您无法通过编程生成“真实”单词,请使用列表并随机选取一些单词。好的,什么算法生成“KITTEN”?
cat
是有效单词,而
ykw
不是。如果不使用字典,算法应该如何知道哪些词是有效的,哪些词是无效的。当你仍然需要使用字典时,只需将一个随机单词涂成粉红色……一个单词(当被人阅读时)不是一个随机的字母集合,字母的顺序决定了它的含义,你无法计算这个顺序——它不是基于任何逻辑规则集(问语言学家!)我可能应该提到我在Windows上。有关英语单词(和一些非单词)的列表,请查看该网站。它有两个单词列表,Diceware列表和Beale列表。
无需使用外部gem
,因此您可以创建一个数组(从您自己的字典)并执行类似[“word1”、“word2”、“word3]的操作。示例,你可以从互联网上复制字典,也可以从linux文件中复制。我改进了我的答案。他在Windows7上工作。我不确定他是否能够使用这些命令/文件,他必须复制字典并使用ruby/powershell命令做些事情。