Is there a method that returns true with the specified probability in Ruby?

Asked 2 years ago, Updated 2 years ago, 88 views

It's just as the title says.
For the time being, we are working on the following items.

def hoge(n)
  # Generate an array with a true pair false of n to 100-n and randomly select one
  ([true]*n + [false]*(100-n)) .sample
end

If not, I would appreciate it if you could advise me on the method.

ruby

2022-09-30 21:22

1 Answers

I have the same question in the English version, so I will quote it.

def choice (weight=50)
  land<=weight/100.0
end

How about

https://stackoverflow.com/questions/16965686/how-to-randomly-choose-true-false-w-weight-in-ruby


2022-09-30 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.