All Projects → viliwonka → WeightedRandomSelector

viliwonka / WeightedRandomSelector

Licence: MIT license
Very fast C# class for weighted random picking.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to WeightedRandomSelector

Flutter photo
Pick image/video from album by flutter. Support ios and android. UI by flutter, no native.
Stars: ✭ 285 (+143.59%)
Mutual labels:  selector, picker
Time Selector
Android Time Selector Library
Stars: ✭ 285 (+143.59%)
Mutual labels:  selector, picker
linear-vs-binary-search
Comparing linear and binary searches
Stars: ✭ 28 (-76.07%)
Mutual labels:  binary-search, linear-search
cortexm-AES
high performance AES implementations optimized for cortex-m microcontrollers
Stars: ✭ 18 (-84.62%)
Mutual labels:  fast, optimized
Pd Select
vue components ,like ios 3D picker style,vue 3d 选择器组件,3D滚轮
Stars: ✭ 101 (-13.68%)
Mutual labels:  selector, picker
Mobile Select
手机移动端选择组件 支持是否级联/单选到多选/可异步更新数据等..
Stars: ✭ 829 (+608.55%)
Mutual labels:  selector, picker
GWLPhotoSelector
相册照片多张选择控件
Stars: ✭ 50 (-57.26%)
Mutual labels:  selector, picker
React Native Modal Dropdown
A react-native dropdown/picker/selector component for both Android & iOS.
Stars: ✭ 1,103 (+842.74%)
Mutual labels:  selector, picker
Fluent-Random-Picker
Fluent Random Picker is a nice, performant, fluent way to pick random values. Probabilities can be specified, values can be weighted.
Stars: ✭ 26 (-77.78%)
Mutual labels:  random, picker
Random-Name-Picker
Simple, beautiful Android app to help you choose from a list of names at random. Downloaded 560,000+ times on Google Play with a 4.5+ rating after 3,500+ reviews.
Stars: ✭ 37 (-68.38%)
Mutual labels:  random, picker
my-picker
高仿IOS的PickerView的JavaScript插件,3D形式的滚轮选择器,同时支持最多三级联动的选择效果,支持pc端和移动端两种平台浏览器。
Stars: ✭ 48 (-58.97%)
Mutual labels:  picker
chconn
Low-level ClickHouse database driver for Golang
Stars: ✭ 152 (+29.91%)
Mutual labels:  fast
nekros
NekRos is an Open-Source Ransomeware, with advanced Features, Which Looks Like Wannacry and Has C&C Server which can be Used to Retrive KEY
Stars: ✭ 84 (-28.21%)
Mutual labels:  fast
vertx-start
简单地、快速地启动vert.x的手脚架,保留了vert.x原汁原味的开发方式
Stars: ✭ 102 (-12.82%)
Mutual labels:  fast
fast-speedtest-api
fast.com API / CLI tool
Stars: ✭ 138 (+17.95%)
Mutual labels:  fast
Random-Number-Generator
A clean, simple random number generator for Android. Downloaded 180,000+ times and rated 2,000+ times on Google Play with 4.7+ average rating.
Stars: ✭ 30 (-74.36%)
Mutual labels:  random
javascript-strong-password-generator
JavaScript Strong Password Generator: based on Jeff Atwood's Post "Password Rules Are Bullshit".
Stars: ✭ 21 (-82.05%)
Mutual labels:  random
Chocobo-Date-Range-Picker
🗓️ Component - The Date Range Picker easier to use in AngularJS.
Stars: ✭ 23 (-80.34%)
Mutual labels:  picker
ng-data-picker
🏄🏼 A data picker based on Angular 4+ (like native datetime picker of iOS)
Stars: ✭ 24 (-79.49%)
Mutual labels:  picker
android-gcc-toolchain
Enable you to use NDK's standalone toolchain easily, quickly and magically for cross-compile
Stars: ✭ 49 (-58.12%)
Mutual labels:  fast

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();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].