Git Product home page Git Product logo

random_choices's Introduction

random_choices

This is similar in functionality to random.choices built into the standard Python library, with improvements to speed and readability.

Why this over random.choices?

First off, it improves readability over even a simple implementation of random.choices. Let's say I have four choices, each with the same probability of being picked except the last, which has double the relative probability of being picked. This is how one would use random.choices to accomplish this:

import random

population = ["first choice", "second choice", "third choice", "fourth choice"]
weights = [1.0, 1.0, 1.0, 2.0]

result = random.choices(population=population, weights=weights, k=1)

The main problem with this solution is that the weights are not visually represented alongside the values to be picked. This decreases readability, and forces the reader to speculate that the indices of the population members and weights are significant.

A secondary problem is that if you need to pick again and neither the population nor weights have changed, you'll find internally that a lot of unnecessary calculation is done. Namely, the accumulation of the weights.

random_choices solves both of these problems, allowing the population to be initialized visually alongside their weights and caching the calculation-intensive steps so that if you have to make another selection when the population or weights have not changed there will not be a significant increase in runtime.

How do I use it?

This is a simple solution to the above problem:

from random_choices import Randomizer, WeightedChoice

population = [
    WeightedChoice("first choice",  weight=1.0),
    WeightedChoice("second choice", weight=1.0),
    WeightedChoice("third choice",  weight=1.0),
    WeightedChoice("fourth choice", weight=2.0)
]
randomizer = Randomizer(population)

randomizer.choices(1, replace=True)

random_choices's People

Contributors

jon-edward avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.