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 8110891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:55:12+00:00 2026-06-06T01:55:12+00:00

Suppose I want to store N samples (each sample takes up a significant portion

  • 0

Suppose I want to store N samples (each sample takes up a significant portion of memory), that should form a representative collection of a total of M>>N samples that are presented to me sequentially. I do not know M in advance, and can only keep N samples in memory simultaneously.

Here, representative, means that each out of the M samples should have equal probability to be stored.

  • 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-06T01:55:16+00:00Added an answer on June 6, 2026 at 1:55 am

    This problem is known as reservoir sampling and there is a very efficient O(M)-time, O(N)-space algorithm for it. The algorithm works as follows: at each point, maintain a “guess” of which N elements you want to choose. Initially, choose the first N elements. Then, after seeing the kth element of the sequence, choose a random number between 1 and k, inclusive. If the number chosen is in the range 1..N, replace the indexed “guessed” item with the current item; otherwise do nothing. You can show using a quick inductive argument that this will sample N elements uniformly at random and with one pass over the data.

    Hope this helps!

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

Sidebar

Related Questions

I am using boost::tuple for my code. Suppose that I want to store an
Ok, suppose I want to store the address 0xcdddd298 in a C string so
Suppose I want to add two buffers and store the result. Both buffers are
I want to store several different methods in an array in Ruby. Suppose I
Suppose I have a string 'johndoe@hotmail.com' . I want to store the string before
Suppose I want to put objects that identify a server into a stl set
Suppose i want to store 3 lines in a file both in python and
Suppose I have this database table (some sample code below) that stores the relationship
Suppose you want to write a program that implements a simple phone book. Given
Suppose I want to store thousands of days in a table how will I

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.