简体   繁体   English

如何在 Ruby (v2.6.3) 中获取随机数

[英]How can I get a random number in Ruby (v2.6.3)

I'm doing an assignment in which I have to make a simple card game.我正在做一项任务,我必须制作一个简单的纸牌游戏。 The tutor has provided me with a method to generate a random card - which I'm not allowed to change:导师为我提供了一种生成随机卡片的方法 - 我不允许更改:

def random_card
  cards = ["two", "three", "four", "five", "six", "seven",
           "eight", "nine", "ten",
           "jack", "queen", "king", "ace"]

  cards[rand(13)]
end

And I have tried to make a method that adds cards to the players hand, calling the random_card method each time:我尝试制作一种方法,将卡片添加到玩家手中,每次调用random_card方法:

def move(random_card)
  player_hand = []
  while true do
    puts "Make a move. Enter 'hit' or 'stick' "
    choice = gets.chomp
    if choice == "stick"
      break
    elsif choice == "hit"
      # this is currently giving me the same card for each "hit"
      # but a different one each time I run the code.
      player_hand.push(random_card)
    end
  end
  # TODO change this to return
  puts player_hand
end

Each time I `hit' the same card gets added to the hand, My question is how to prevent this and for the method to return a "new" random card each time we run through the loop.每次我“击中”同一张牌时,我的问题是如何防止这种情况发生,以及每次运行循环时返回“新”随机牌的方法。

Thanks.谢谢。

You've used the name random_card for two different things: 1) the method given you by your instructor;您已将random_card名称用于两种不同的random_card :1) 教师给您的方法; and 2) the name of the argument to your move method.和 2) move方法的参数名称。

The second usage defines a local variable, which masks (or shadows) the method name.第二种用法定义了一个局部变量,它屏蔽(或隐藏)了方法名称。 In other words, within the move method random_card refers to whatever value you passed in when you called move .换句话说,在move方法中random_card指的是您在调用move时传入的任何值。 It does not call the method.调用该方法。

Here's a simple illustration:这是一个简单的说明:

def foo
  %w(hi hey howdy)[rand 3]
end

def bar1
  puts foo
end

def bar2(foo)
  puts foo
end

5.times { bar1 }  # produces a random selection of hi's, hey's, and howdy's
puts
5.times { bar2 "Help!  I'm stuck!" }  # prints the passed message 5 times
puts
5.times { srand 12345; bar1 }  # produces "howdy" 5 times

Given what you've shown, you probably don't need an argument to move .鉴于您所展示的内容,您可能不需要参数来move If you do, give it its own name.如果你这样做,给它自己的名字。 Also make sure you aren't messing with srand somewhere else in your program.还要确保您没有在程序中的其他地方弄乱srand


Yet another alternative would be to indicate that you want to use the method rather than the argument with the same name by using parentheses.另一种选择是通过使用括号来指示您要使用该方法而不是具有相同名称的参数。 With the example above, change bar2 to:对于上面的示例,将bar2更改为:

def bar2(foo)
  puts foo()
end

The empty parentheses make it clear this is an invocation of the method foo , not a reference to the (now unused) argument foo .空括号清楚地表明这是对方法foo的调用,而不是对(现在未使用的)参数foo的引用。

here is the simple solution to your problem这是您问题的简单解决方案

def random_card
  cards = ["two", "three", "four", "five", "six", "seven",
           "eight", "nine", "ten",
           "jack", "queen", "king", "ace"]

  cards[rand(13)]
end

def move(callback)
  player_hand = []
  while true do
    puts "Make a move. Enter 'hit' or 'stick' "
    choice = gets.chomp
    if choice == "stick"
      break
    elsif choice == "hit"
      player_hand.push(callback.call)
    end
  end
  puts player_hand
end

and you can call this method as你可以调用这个方法作为

move(method(:random_card))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查ruby-2.6.3使用的是jemalloc? 我安装了ruby-2.6.3作为$ rvm install 2.6.3 -C --with-jemalloc - How to check ruby-2.6.3 is using jemalloc ? I installed ruby-2.6.3 as $ rvm install 2.6.3 -C --with-jemalloc 如何获得Ruby中的随机数 - How to get a random number in Ruby Ruby -v 显示 2.6.3,但终端说我在尝试运行 rails 控制台时正在运行 2.3.7 - Ruby -v shows 2.6.3, but Terminal says I'm running 2.3.7 when trying to run rails console 我如何在ruby中重用一个随机数? - How would I reuse a random number in ruby? 如何在 amazon linux 中将 ruby​​ 版本升级到 2.6.3 - How to upgrade ruby version to 2.6.3 in amazon linux 为什么我使用 Ruby 2.6.3 在 irb 中得到未定义的方法 `mktmpdir' for Dir:Class? - Why do I get undefined method `mktmpdir' for Dir:Class in irb using Ruby 2.6.3? 如何在Ruby中获得具有给定离散分布的随机数 - How to get a random number with a given discrete distribution in Ruby 如何解决'你的Ruby版本是2.6.3,但是你的Gemfile指定的是2.7.4'怎么办? - How to resolve 'Your Ruby version is 2.6.3, but your Gemfile specified 2.7.4" What should I do? 在Ruby语言中,如何获取字符串中的行数? - In Ruby language, how can I get the number of lines in a string? 如何在 ruby 中生成一个随机的 10 位数字? - How do I generate a random 10 digit number in ruby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM