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

  • Home
  • SEARCH
  • 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 929013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:06:07+00:00 2026-05-15T20:06:07+00:00

Page 120 of Programming Pearls 1st edition presents this algorithm for selecting M equally

  • 0

Page 120 of Programming Pearls 1st edition presents this algorithm for selecting M equally probable random elements out of a population of N integers.

InitToEmpty
Size := 0
While Size < M do
  T := RandInt(1,N)
  if not Member(T)    
     Insert(T)
     Size := Size + 1

It is stated that the expected number of Member tests is less than 2M, as long as M < N/2.

I’d like to know how to prove it, but my algorithm analysis background is failing me.

I understand that the closer M is to N, the longer the program will take, because the result set will have more elements and the likelihood of RandInt selecting an existing one will increase proportionally.

Can you help me figuring out this proof?

  • 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-05-15T20:06:07+00:00Added an answer on May 15, 2026 at 8:06 pm

    I am not a math wizard, but I will give it a rough shot. This is NOT guaranteed to be right though.

    For each additional member of M, you pick a number, see if it’s there, and if is add it. Otherwise, you try again. Trying something until you’re successful is called a geometric probability distribution.

    http://en.wikipedia.org/wiki/Geometric_distribution

    So you are running M geometric trials. Each trial has expected value 1/p, so will take expected 1/p tries to get a number not already in M. p is N minus the number of numbers we’ve already added from M divided by N (i.e. how many unpicked items / total items). So for the fourth number, p = (N -3) / N, which is the probability of picking an unused number, so the expected number of picks for the third number is N / N-3 .

    The expected value of the run time is all of these added together. So something like

    E(run time) = N/N + N/(N -1) + N/(N -2 ) … + N/ (N-M)

    Now if M < N/2, then the last element in that summation is bounded above by 2. ((N/N/2) == 2)). It’s also obviously the largest element in the whole summation. So if the biggest element is two picks, and there are M elements being summed, the EV of the whole run time is bounded above by 2M.

    Ask me if any of this is unclear. Correct me if any of this is wrong 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This page from Adobe says to add a wmode parameter and set its value
This page mentions how to trunc a timestamp to minutes/hours/etc. in Oracle. How would
This page displays beautifully in firefox but i get all kinds of problems when
The page Protecting Your Cookies: HttpOnly explains why making HttpOnly cookies is a good
Upon page load I want to move the cursor to a particular field. No
My page deals with many Store objects, each of them has a field called
Each page of my site has 10 (almost) identical divs, varying only in the
I have a page where there is a column and a content div, somewhat
How do I page results in SQL Server 2005? I tried it in SQL
I have a simple page with my ScriptManager and my UpdatePanel , and my

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.