Objective: You have a list of things with a popularity score and want to get a random item from the list, but you don't want just ANY random item. You want it to be a generally popular item. How it works: You take a key for the weight, we'll say `popularity` (0-100). And you have maybe 1,000 rows of content, each with said weight. You sum up all the popularities to variable X. You set a random number between 0 and X; call it R. You iterate through your 1,000 rows (Y), and each iteration you subtract Y's `popularity` from your random number, R, and when R <= 0... you use that row. --- Quick Example: https://jsfiddle.net/rL6a01ku/ Press "RUN" over and over to see the numbers change, but basically still stay the same.
B Trees. You can call them a set of algorithms or a data structure if you prefer, but its core idea can be expressed as an algorithm: making the root of the tree emerge from leaves. It's kind of democratic!
Edit: it's also a massively practic idea that powers most databases.
Comments (491) 209 threads
Earlier commentsThis makes so much of online trust and verification possible.
Objective: You have a list of things with a popularity score and want to get a random item from the list, but you don't want just ANY random item. You want it to be a generally popular item.
How it works: You take a key for the weight, we'll say `popularity` (0-100). And you have maybe 1,000 rows of content, each with said weight.
You sum up all the popularities to variable X. You set a random number between 0 and X; call it R. You iterate through your 1,000 rows (Y), and each iteration you subtract Y's `popularity` from your random number, R, and when R <= 0... you use that row.
---
Quick Example: https://jsfiddle.net/rL6a01ku/
Press "RUN" over and over to see the numbers change, but basically still stay the same.
Edit: it's also a massively practic idea that powers most databases.
All comments shown.
Couldn't load more comments. Check your connection and try again.