Ruby - 随机数是每次相同的随机数

时间:2013-10-26 17:57:39

标签: ruby random

wordList = ['1930', '1931', '1932', '1933', '1934', '1935', '1936', '1937', '1938',        '1939', '1940']
wordLen = wordList.length
wordRand = rand(wordLen)
year = wordList[wordRand]

这里非常新的...... year的行为是这样的,每次我运行程序时,它都会从wordList中选择一个随机字符串。问题是它需要特定的随机选择数字并将其设置为等于year。因此,对于程序的每个实例,year在每次调用时都是列表中的相同字符串。我怎样才能让它每次都选择不同的数字?

puts 'Why hello there, dear! Grandma is, SO, happy to see you!'
response = gets.chomp
  while response != 'BYE'
    if response == response.upcase
      puts 'NO, NOT SINCE ' + year + '!'
      response = gets.chomp
    else
      puts 'WHAT? SPEAK UP, SONNY!'
      response = gets.chomp
    end
  end
  if response == 'BYE'
    puts 'OKAY, BYE DEAR!!'
  end

编辑:添加上下文

1 个答案:

答案 0 :(得分:1)

您似乎只为year生成一个值,然后在循环中重复使用它。如果您想要不同的号码,请将呼叫置于循环中{.1}}。