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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:32:27+00:00 2026-05-15T08:32:27+00:00

I have a scenario where I need to show a different page to a

  • 0

I have a scenario where I need to show a different page to a user for the same url based on a probability distribution,

so for e.g. for 3 pages the distribution might be

page 1 - 30% of all users
page 2 - 50% of all users
page 3 - 20% of all users

When deciding what page to load for a given user, what technique can I use to ensure that the overall distribution matches the above?

I am thinking I need a way to choose an object at “random” from a set X { x1, x2….xn } except that instead of all objects being equally likely the probability of an object being selected is defined beforehand.


Thanks for the input everyone, after doing some prototyping, this is what I ended up using

private static int RandomIndexWithPercentage(Random random, int[] percentages) {
    if (random == null) {
        throw new ArgumentNullException("random");
    }

    if (percentages == null || percentages.Length == 0) {
        throw new ArgumentException("percentages cannot be null or empty", "percentages");
    }

    if(percentages.Sum() != 100) {
        throw new ArgumentException("percentages should sum upto 100");
    }

    if (percentages.Any(n => n < 0)) {
        throw new ArgumentException("percentages should be non-negative");
    }

    var randomNumber = random.Next(100);
    var sum = 0;
    for (int i = 0; i < percentages.Length; ++i) {
        sum += percentages[i];
        if (sum > randomNumber) {
            return i;
        }
    }

    //This should not be reached, because randomNumber < 100 and sum will hit 100 eventually
    throw new Exception("Unexpected");
} 
  • 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-15T08:32:27+00:00Added an answer on May 15, 2026 at 8:32 am

    Generate a number 0-9. If the number is less than 3, give them page one. If it’s less than 8, give them page two, or else give them page three.


    Some code, to get you started:

    private int ChoosePage()
    {
        int[] weights = new int[] { 3, 5, 2 };
        int sum = 0;
        int i;
        for (i = 0; i < weights.Length; i++)
            sum += weights[i];
        int selection = (new Random()).Next(sum);
        int count = 0;
        for (i = 0; i < weights.Length - 1; i++)
        {
            count += weights[i];
            if (selection < count)
                return i;
        }
        return weights.Length - 1;
    }
    

    Note that weights doesn’t have to add up to anything in particular. If sum = 100, then weight[i] is th percent chance of getting page i. If it doesn’t, however, it’s just relative – if weight[i] is twice weight[j], then page i will get twice as many hits as page j. This is nice because you can arbitrarily increase or decrease page traffic without recalculating anything. Alternatively, you could make sure the sum is always N, and hardcode N in, rather than summing all the values every time. There are a lot more optimizations you could do, I’m sure.

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

Sidebar

Related Questions

I have one scenario where I need to select all files having aliencoders.numeric-digits like
I have this scenario. I need to show 3 list boxes on a ASP.NET
I have the next scenario in my UrlMappings.groovy: /user/$action? (controller:user) /admin/$action? (controller:user) 500(controller:error, action:show)
I have a scenario where I need to show a bunch of buttons only
I have a scenario where I need to show the preview of form in
I have a case where I need to show only the top rows based
I have a scenario where I need to pull up delivery dates based on
I have scenario where I need to host a web service (WCF) on Azure
I have a scenario where I need to load properties from database or java
I have a scenario where I need to do the following in a transaction:

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.