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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:59:09+00:00 2026-05-13T21:59:09+00:00

I have a case where I need to select a random item, but I

  • 0

I have a case where I need to select a random item, but I don’t know the total number of items and I don’t want to build a huge array then pick an item out. For example, this is what I have right now:

List<string> items;
while (true)
{
    string item = GetNextItem();
    if (item == null)
        break;
}
int index = random.GetNext(0, items.count);

As you can see, I’m building a gigantic collection that I really don’t need, I just need a random number between 0 and the number of items. Here is what I am thinking of doing, and it works, but I’d like to know if any of the experts out there can find a fault with it:

int index = -1;
int total;
string selectedItem;
while (true)
{
    string item = GetNextItem();
    if (item == null)
        break;

    ++total;
    int rnd = random.Next(0, total);
    if (rnd == total- 1)
    {
        index = total- 1;
        selectedItem = item;
    }
}

This gives me my index number, and the randomly selected item. My thinking behind this is that when there are 3 total items, for example, I pick a random number between 0 and 2 (inclusive) and if it’s equal to 2 I use the new item as the selected item, if not just ignore it. As the total number of items increases, each new item’s chance of being selected decreases accordingly.

Is this method “good”? Is it as “random” as building the array and picking an item out later? Is it as fast as it can be? Please guide me through my ignorance in random numbers. 🙂

  • 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-13T21:59:10+00:00Added an answer on May 13, 2026 at 9:59 pm

    What you’re doing will work.

    Here’s a restating of it that might make the algorithm slightly more clear:

    1. Select the first item, there is a
      100% chance it will be the current
      selection
    2. If there is a second item,
      there is a 1/2 chance it will
      replace the current selection (If you do the math, then it’s a 50% chance it will be the first item, and a 50% chance it will be the second item)
    3. If
      there is a third item, there is a
      1/3 chance it will replace the
      current selection (again, the math the probability for each item being 1/3)
    4. If there is a
      fourth item, there is a 1/4 chance
      it will replace the current
      selection
    5. … etc …

    Note that you should be able to compute a 1/x chance by saying rand.Next(0,x) == 0 (or any other integer between 0 and x - 1 inclusive; you don’t have to bother using total - 1.

    It’s actually a pretty neat approach; initially I thought there wasn’t going to be any good way of doing what you were asking!

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

Sidebar

Related Questions

Have a use case wherein need to maintain a connection open to a database
I have a case where I need to translate (lookup) several values from the
I have a case where I need to serve raw swf files to the
I have a business case whereby I need to be able to specify my
I have a CakePHP 1.2 application. I'm running into the case where I need
Imagine this case where I have an object that I need to check a
In my specific case, I have two kinds of messages that I need to
So I have case statements grouped together here, but I have a few case
I have a fairly simple task in which I need to select rows with
I have a case where a 3rd party ad is bleeding through my modal

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.