Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9251417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:41:36+00:00 2026-06-18T10:41:36+00:00

In the following Ruby code I have two methods d100_in_detect and d100_out_detect which return

  • 0

In the following Ruby code I have two methods d100_in_detect and d100_out_detect which return an Array of unique elements (numbers for simplicity) contained in ary according to the result of d100.

def d100
  1 + ( rand 100 )
end

def d100_in_detect( ary )
  choice = [ ]
  100.times do
    choice.push ary.detect { |el| d100 <= el }
  end
  choice.uniq.sort
end

def d100_out_detect( ary )
  choice  = [ ]
  numbers = [ ]

  100.times do
    numbers.push d100
  end

  numbers.each do |i|
    choice.push ary.detect { |el| i <= el }
  end

  choice.uniq.sort
end

As you can see the difference between the two methods is that in the first d100 is called inside detect‘s block, while in the second 100 random numbers are stored in numbers Array and then are used as it happens in d100_in_detect.

Let’s suppose I call the two methods as follows

ary = [ ]
50.times do |i|
  ary.push i * 5 
end

puts '# IN DETECT #'
print d100_in_detect ary
puts

puts '# OUT DETECT #'
puts d100_out_detect ary
puts

A typical output is the following.

# IN DETECT #
[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 ]
# OUT DETECT #
[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 ]

I can’t figure out why the two methods return such different results.
Is there any implication in calling d100 method in detect‘s block?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:41:37+00:00Added an answer on June 18, 2026 at 10:41 am

    Right.

    First thing I did was to modify your sample script:

    def d100_in_detect( ary )
      choice = [ ]
      numbers = []
      100.times do
        var = d100
        numbers << var
        choice.push ary.detect { |el| var <= el }
      end
      puts numbers.inspect
      choice.uniq.sort
    end
    
    def d100_out_detect( ary )
      choice  = [ ]
      numbers = [ ]
    
      100.times do
        numbers.push d100
      end
      puts numbers.inspect
    
      numbers.each do |i|
        choice.push ary.detect { |el| i <= el }
      end
    
      choice.uniq.sort
    end
    

    As you can see, all I did was assign the result of the d100 to a temporary variable to see what was happening….and the ‘bug’ went away! My return values were suddenly identical. Hmm.

    Then, it occurred to me exactly what was going on. When you ‘cache’ the variable (as you do in the second example), you guarantee you have a spread of 100 numbers.

    When you iterate through the block, for each number in the block, you execute the d100 again. Hence, the first one has vastly more calls than the second one…but you also require the randomly generated number at the time that number was called to be greater than the number (whereas, if you randomly generate 100 with 2, you can guarantee it will hit the 100 at some point). This drastically bias’s your script towards the lower numbers!

    For an example, run:

    @called_count = 0
    def d100
      @called_count += 1
      1 + ( rand 100 )
    end
    
    def d100_in_detect( ary )
      choice = [ ]
      numbers = []
      100.times do
        choice.push ary.detect { |el| d100 <= el }
      end
      puts @called_count.inspect
      @called_count = 0
      choice.uniq.sort
    end
    
    def d100_out_detect( ary )
      choice  = [ ]
      numbers = [ ]
    
      100.times do
        numbers.push d100
      end
            puts @called_count.inspect
      @called_count = 0
    
      numbers.each do |i|
        choice.push ary.detect { |el| i <= el }
      end
    
      choice.uniq.sort
    end
    

    I get

    # IN DETECT #
    691
    # OUT DETECT #
    100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the simple following code, which is working in a ruby (not rails)
I have the following bit of ruby code which works fine require 'WIN32OLE' excel
If I have the following piece of Ruby code: class Blah def self.bleh @blih
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
What does the following code mean in Ruby? ||= Does it have any meaning
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
I'm playing around with Ruby and I have written the following code: module IdAndNameRedefine
I have the following Ruby code: local_var = Hello def hello puts local_var end
Using Ruby 1.9.2, I have the following Ruby code in IRB: > r1 =
I have the following Ruby code: require 'rubygems' require 'rest_client' url = 'http://asdf.com' response

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.