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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:40:28+00:00 2026-05-12T11:40:28+00:00

I would like to learn how to select weighted items. For example : I

  • 0

I would like to learn how to select weighted items. For example : I want to fetch questions from a pool but if some one can’t give right answer to a question, that causes this question to double its weight and increase the probability of being selected again later on.

  • 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-12T11:40:28+00:00Added an answer on May 12, 2026 at 11:40 am

    Have a class which keeps the item:weight pairs (key=item:value=weight) in a hash table.

    The class should also maintain a total_weight variable, which is the sum of all the weights in the hash table. The class’ methods to add_item, remove_item, and update_weight for an item should keep the total_weight updated. This avoids having to recalculate the total for every choice.

    To choose an item:
    Use a random number such that 1<=random_number<=total_weight.
    Iterate over the item:weight pairs in the hash table, summing the weights until the random number is <= that running sum. When that happens, the key of the pair you’re on is the chosen item.

    This is like rolling an imaginary die whose size is the sum of all the weights. For every roll, each item has its own range of numbers on the die, with the size of each range equal to its item’s weight. If the roll result falls within an item’s range, that item is the chosen one.

    Editing to add the following sample code after the request in the comment below. Tested this with Python 2.5.2:

    from random import randint  # Import randint function from random module.
    
    class WeightedCollection(object):
        def __init__(self):
            self.total_weight = 0
            self.items = {}  # This is a python dictionary == a hash table
        def add_item(self, item, weight):
            self.items[item] = weight
            self.total_weight += weight
        def remove_item(self, item):
            self.total_weight -= self.items[item]  # Subtracts the weight.
            del(self.items[item])
        def update_weight(self, item, new_weight):
            self.total_weight += (new_weight - self.items[item])
            self.items[item] = new_weight
        def get_random_item(self):
            ''' Returns random selection but weighted by item weights. '''
            # Result of call below is 1 <= random_number <= self.total_weight...
            random_number = randint(1, self.total_weight)
            sum_so_far = 0
            # For every item and its weight...
            for item, weight in self.items.iteritems():
                sum_so_far += weight
                if random_number <= sum_so_far:
                    return item
    
    # Usage demo...
    
    questions = WeightedCollection()
    
    questions.add_item('What is your name?', 1)
    questions.add_item('What is your favorite color?', 50)
    questions.add_item('What is the meaning to life?', 100)
    
    print 'Here is what the dictionary looks like:'
    print questions.items
    print ''
    print "Total weight:", questions.total_weight
    print ''
    print 'Some sample random picks...'
    for i in range(5):
        print questions.get_random_item()
    

    And here is the output:

    Here is what the dictionary looks like:
    {'What is the meaning to life?': 100, 'What is your name?': 1, 'What is your favorite color?': 50}
    
    Total weight: 151
    
    Some sample random picks...
    What is your favorite color?
    What is the meaning to life?
    What is the meaning to life?
    What is your favorite color?
    What is the meaning to life?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to learn about HTML forms. For example, I have 2 input
I would like to show several columns from the orders table, one of which
I would like to select streamitem_id from my insert.php and add it to my
I'm trying to learn SQL, using PostgreSQL 9.1.3. I would like to understand some
I would like to learn how to use NUnit. I learn best by reading
I would like to learn how setup prefix routing like there is in cakephp
I would like to learn how to use binding functions. Here is the idea:
I would like to learn how I can use nested template tags where the
I would like to learn about games (strategy) algorithms especially about how do enemies
I would like to learn SAP ERP. Is there a SAP ERP student edition?

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.