ScratchData LogoScratchData
Back to iaobardar's profile

Weighted Random Choice (Alias Method)

IAiaobardar•Created December 17, 2021
Weighted Random Choice (Alias Method)
8
5
85 views
View on Scratch

Instructions

This is a demo of an algorithm I found while searching online. It queries a weighted random choice in constant time. It's a funny algorithm that does a bit of preprocessing to link probabilities, so it can sample them uniformly twice and get a non-uniform output. See Inside to alter the weights. (Though my code is a mess.) The diagrams on the left side of the stage are the weights and the aliased weights. You can see how there is the same amount of each color, just cut into parts. I should probably visualize the output the same as the probabilities. Maybe I'll do that in an update.

Description

Sources and explanations: * https://blog.bruce-hill.com/a-faster-weighted-random-choice * https://www.keithschwarz.com/darts-dice-coins/ * https://en.wikipedia.org/wiki/Alias_method

Project Details

Project ID618717777
CreatedDecember 17, 2021
Last ModifiedDecember 31, 2021
SharedDecember 31, 2021
Visibilityvisible
CommentsAllowed