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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:20:27+00:00 2026-05-12T17:20:27+00:00

I wish to select a random word from a list where the is a

  • 0

I wish to select a random word from a list where the is a known chance for each word, for example:

Fruit with Probability

Orange 0.10
Apple 0.05
Mango 0.15
etc

How would be the best way of implementing this? The actual list I will take from is up to 100 items longs and the % do not all tally to 100 % they do fall short to account for the items that had a really low chance of occurrence. I would ideally like to take this from a CSV which is where I store this data. This is not a time critical task.

Thank you for any advice on how best to proceed.

  • 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-12T17:20:28+00:00Added an answer on May 12, 2026 at 5:20 pm

    You can pick items with weighted probabilities if you assign each item a number range proportional to its probability, pick a random number between zero and the sum of the ranges and find what item matches it. The following class does exactly that:

    from random import random
    
    class WeightedChoice(object):
        def __init__(self, weights):
            """Pick items with weighted probabilities.
    
                weights
                    a sequence of tuples of item and it's weight.
            """
            self._total_weight = 0.
            self._item_levels = []
            for item, weight in weights:
                self._total_weight += weight
                self._item_levels.append((self._total_weight, item))
    
        def pick(self):
            pick = self._total_weight * random()
            for level, item in self._item_levels:
                if level >= pick:
                    return item
    

    You can then load the CSV file with the csv module and feed it to the WeightedChoice class:

    import csv
    
    weighed_items = [(item,float(weight)) for item,weight in csv.reader(open('file.csv'))]
    picker = WeightedChoice(weighed_items)
    print(picker.pick())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SELECT i.*, i.id IN ( SELECT id FROM w WHERE w.status='active') AS wish FROM
I have a table that I wish to select a subset of columns from
I wish to do the following: select * into tmptbl from sometable EXCEPT 'sometable'
I wish to run an SQL select statement similar to this SELECT * FROM
I have list of items in my dropdown. Now I wish to select item
I wish to get the latest date per product ID: SELECT ProductID, Max(Date) as
I wish to do something like this: DECLARE @IgnoreNulls = 1; SELECT Col1, Col2
I have an app where users select images they wish to print, the print
Using a linq query/method chainging I wish to select just the first 2 Point
What is the correct SQL statement if i wish to select the biggest 5

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.