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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:29:12+00:00 2026-06-18T03:29:12+00:00

I wish to use the answer provided in this post to randomly choose unique

  • 0

I wish to use the answer provided in this post to randomly choose unique items from a list.

Following the method described, in each iteration of my loop I generate a probability value which is the percentage chance of the current item being picked from the list.

What I need to know is how do I use this percentage value to pick the item (or not).

Here is the code I have, with remainingIndices being a List<int>

for (var i = 0; i < remainingIndices.Count; i++)
{
    var probability = pixelsToAdd / (float)(remainingIndices.Count - i);
}

pixelsToAdd is 120 and remainingIndices.Count is 3600.
The probability values I am getting start at 0.0333333351

The solution should be flexible to work with a much wider range of values, preferably any values.

Thanks

Comment

For future readers of this question I should clarify that at first I thought the probability value was some percentage between 0 and 100 but in reality it’s a value between 0 and 1 and so matches up perfectly with the return value of Random.NextDouble() which therefore can be used for comparison as described in the answers below.

  • 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-18T03:29:13+00:00Added an answer on June 18, 2026 at 3:29 am

    To use your probability, compare it with a sample from random variable following a uniform distribution on [0, 1].

    if (Random.NextDouble() <= probability)
        // Take the ith element in the list
    

    You resulting loop will be:

    List<???> selectedItems = new List<???>();
    for (var i = 0; i < remainingIndices.Count; i++)
    {
        var probability = pixelsToAdd / (float)(remainingIndices.Count - i);
        if (Random.NextDouble() <= probability)
        {
            selectedItems.Add(items[i]);
            pixelsToAdd--;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to use the method with the following signature: exec(String command, String[] envp,
I wish to use sp_start_job to start a job from within an insert trigger
I wish I would find an answer for this. I have searched and searched
I'm pretty sure an hour after I post this I may find the answer
The following question and answer addresses the use of an out parameter in a
I wish to use Spring MVC to provide a REST web service. However, running
I wish to use a COM dll in my C++ library. The way I
I wish to use the Translator classes of Zend framework in my app, but
I wish to use common CRC logic in a VB.NET or C# application as
I wish to use JSON.Net on a brand spankin new Mono class library (say

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.