GithubHelp home page GithubHelp logo

viliwonka / weightedrandomselector Goto Github PK

View Code? Open in Web Editor NEW
127.0 4.0 16.0 289 KB

Very fast C# class for weighted random picking.

License: MIT License

C# 100.00%
csharp random picker selector weighted fast optimized binary-search linear-search unity3d

weightedrandomselector's Introduction

Weighted Random Selector

Description

This repository contains code for selecting items randomly based on weights. It is very commonly used thing in video game development and it is suprisingly tricky to get it right.

It was designed to be:

  • fast for smaller and bigger collections,
  • readable,
  • easy to use,
  • numerically precise
  • thread-safe selecting

Use cases

  • Item dropping from resources, enemies
  • Damage giving / taking
  • Selecting random events to happen
  • Procedural quest generation
  • Picking random sound effect to play on event
  • Casino games: Roulette, Slot machine (although I believe those casino games are weighted uniformly)
  • Random state machines / Markov chain

How to use

You have two options:

DynamicRandomSelector
  • Easier to use, since it contains both Construction and Random Picking parts
  • Can be modified on the fly (added or removed items), since internal collection is List
  • Around 2x slower, since internal collection is List
StaticRandomSelector
  • Harder to use
  • Separate component for Construction (StaticRandomSelectorBuilder) and RandomPicking (StaticRandomSelector)
  • Faster, since internal collection is Array
  • If you really need extra speed, remove all functions calls with hardcoding all functions

How it works?

Construction
  1. You input items, and every item has it's own weight (un-normalized)
  2. Array of weights is normalized, so that it becomes array of probabilities/masses (sum of 1)
  3. This array of probabilities get converted to (discrete) Cumulative Distribution Array
Random picking
  1. For each random pick, uniform random number is generated
  2. Depending on size of collection, linear or binary search is used to select correct index
  3. This index is used to return item from internal array/list.

Minimum of linear search and binary search

This picture demostrates what is happening under hood, to achieve good performance.

alt text

In short

Just use DynamicRandomSelector, and do not forget to call Build method after modifying (adding & removing) it.

Example code

var selector = new DynamicRandomSelector<float>();

// adding items
for (int i = 0; i < 32; i++) {

    var item = i;
    var unnormalizedWeight = Mathf.Sqrt(i + 1); //non zero weight

    selector.Add(item, unnormalizedWeight);
}

// building after modification is complete
selector.Build();

// picking random items
for (int i = 0; i < 10000; i++)
    var randomItem = selector.SelectRandomItem();

weightedrandomselector's People

Contributors

viliwonka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.