Created
August 10, 2015 16:56
-
-
Save ion1/2e45a5199682da97af43 to your computer and use it in GitHub Desktop.
Sample an item from an ordered set with the proportional weights 1, 2, 3, …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Sample an item from an ordered set with the proportional weights 1, 2, 3, …: | |
| tri x = (x·(x+1))/2 | |
| invtri x = (√(8·x+1)−1)/2 | |
| r ← uniform [0,1) | |
| n = number of items | |
| index = floor (invtri (r · tri n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment