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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:25:23+00:00 2026-05-22T11:25:23+00:00

I have a site where users can post and vote on suggestions. On the

  • 0

I have a site where users can post and vote on suggestions. On the from page I initially list 10 suggestions and the header fetches a new random suggestion every 7 seconds.

I want the votes to influence the probability a suggestion will show up, both on the 10-suggestion list and in the header-suggestion. To that end I have a small algorithm to calculate popularity, taking into account votes, age and a couple other things (needs lots of tweaking).

Anyway, after running the algorithm I have a dictionary of suggestions and popularity index, sorted by popularity:

{ S = Suggestion1, P = 0.86  }
{ S = Suggestion2, P = 0.643 }
{ S = Suggestion3, P = 0.134 }
{ S = Suggestion4, P = 0.07  }
{ S = Suggestion5, P = 0.0   }
{ . . .}

I don’t want this to be a glorified sort, so I’d like to introduce some random element to the selection process.

In short, I’d like the popularity to be the probability a suggestion gets picked out of the list.

Having a full list of suggestion/popularity, how do I go about picking 10 out based on probabilities? How can I apply the same to the looping header suggestion?

  • 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-22T11:25:23+00:00Added an answer on May 22, 2026 at 11:25 am

    I’m afraid I don’t know how to do this very fast, but if you have the collection in memory you can do it like this:

    Note that you do not need to sort the list for this algorithm to work.

    1. First sum up all the probabilities (if the probability is linked to popularity, just sum the popularity numbers, where I assume higher values means higher probability)
    2. Calculate a random number in the range of 0 up to but not including that sum
    3. Start at one end of the list and iterate through it
    4. For each element, if the random number you generated is less than the popularity, pick that element
    5. If not, subtract the popularity of the element from the random number, and continue to the next

    If the list is static, you could build ranges and do some binary searches, but if the list keeps changing, then I don’t know a better way.

    Here is a sample LINQPad program that demonstrates:

    void Main()
    {
        var list = Enumerable.Range(1, 9)
            .Select(i => new { V = i, P = i })
            .ToArray();
        list.Dump("list");
    
        var sum =
            (from element in list
             select element.P).Sum();
    
        Dictionary<int, int> selected = new Dictionary<int, int>();
        foreach (var value in Enumerable.Range(0, sum))
        {
            var temp = value;
            var v = 0;
            foreach (var element in list)
            {
                if (temp < element.P)
                {
                    v = element.V;
                    break;
                }
    
                temp -= element.P;
            }
            Debug.Assert(v > 0);
            if (!selected.ContainsKey(v))
                selected[v] = 1;
            else
                selected[v] += 1;
        }
    
        selected.Dump("how many times was each value selected?");
    }
    

    Output:

    list 
    [] (9 items)  
     V  P
     1  1 
     2  2 
     3  3 
     4  4 
     5  5 
     6  6 
     7  7 
     8  8 
     9  9 
    45 45  <-- sum
    
    how many times was each value selected? 
    Dictionary<Int32,Int32> (9 items)  
    Key Value
     1    1 
     2    2 
     3    3 
     4    4 
     5    5 
     6    6 
     7    7 
     8    8 
     9    9 
         45 <-- again, sum
     
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a page on my site where users can post directly to
I have a site in which users can post some questions, so I a
On my site I have a page where users can upload files to go
I have a site where users can post stuff (as in forums, comments, etc)
I have a database driven community web site, where users can post entries. Each
I have an ASP.NET MVC 3 application where users can post suggestions along the
I have a site where users can make posts. The users can be from
Twitter users can login and post comments on my site, as well as new
I have a site where users can log in and check on outstanding support
Let's say I have a site where Users can add Entries through admin panel.

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.