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

  • Home
  • SEARCH
  • 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 901079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:29:59+00:00 2026-05-15T15:29:59+00:00

I have a requirement in my .NET project where I need to select an

  • 0

I have a requirement in my .NET project where I need to select an item from a collection, each item has a Weight (integer from 1 to 10) assigned to it.

I need a random generator that would take this weight into consideration i.e., the higher the weight, the more chances the object would be selected.

Quick copy/paste C# code in case someone stumbles upon this.

    class RandomWeightedSelector<T>
    {
        private List<T> items = new List<T>();

        public void Add(T item, uint weight = 1)
        {
            for (int i = 0; i < weight; i++)
                items.Add(item);
        }

        public T GetRandom()
        {
            return items[new Random().Next(0, items.Count)];
        }
    }
  • 1 1 Answer
  • 1 View
  • 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-15T15:29:59+00:00Added an answer on May 15, 2026 at 3:29 pm

    Here’s an algorithm which doesn’t require adding the items multiple times to a list. It can also work with non-integer weights, although if you’re using NextDouble from System.Random, you’ll have to scale all of the weights to add up to 1, or multiply the value from NextDouble with S to get it in the desired range.

    Given a list L of items (I,W), where I is the item and W is the weight:

    1. Add all of the weights together. Call this sum S.
    2. Generate a random number between 0 and S (excluding S, but including 0). Call this value R.
    3. Initialize a variable to 0 to keep track of the running total. We’ll call this T.
    4. For each item (I,W) in L:
      1. T=T+W
      2. If T > R, return I.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET and C# in my project. I have a requirement where
I have a requirement to pass credentials from one Microsoft .NET Web Application to
I am using Asp.Net/C# in my application,I have a requirement where I need to
I'm using ASP.NET MVC in a project and I have a requirement like this..
I have requirement of creating MS Project object in asp.net c# and exporting image
In my ASP.NET MVC project I have a requirement to return certain pages in
In my ASP.Net C# project I have a requirement to display a dropdown containing
I have a requirement to create a multi level treeview in ASP.Net (with VB)
I have following requirement, I have C#/.Net console application, which refers to 'System.Data.Sqlite.dll' 'System.Data.Sqlite.dll'
I have a requirement for an explicit logout button for users in a ASP.NET

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.