Skip to main content
copy-edited
Source Link
Deduplicator
  • 9.3k
  • 5
  • 34
  • 53

Return get weighted random `list` item by its `weight`

I have, for example, this table

+-----------------+
| fruit  | weight |
+-----------------+
| apple  |   4    |
| orange |   2    |
| lemon  |   1    |
+-----------------+

I need to return a random fruit. But appleapple should be picked 4 times frequently then lemonas frequent as Lemon and 2 times frequently then orangeas frequent as orange.

In more general case it should be f(weight) times frequently.

What is a good general algorithm to implement this behavior?

Or maybe there isare some ready gems on Ruby? :)

PS 
I've implemented current algorithm in Ruby https://github.com/fl00r/pickup

Return random `list` item by its `weight`

I have, for example, this table

+-----------------+
| fruit  | weight |
+-----------------+
| apple  |   4    |
| orange |   2    |
| lemon  |   1    |
+-----------------+

I need to return random fruit. But apple should be picked 4 times frequently then lemon and 2 times frequently then orange.

In more general case it should be f(weight) times frequently.

What is general algorithm to implement this behavior?

Or maybe there is some ready gems on Ruby? :)

PS I've implemented current algorithm in Ruby https://github.com/fl00r/pickup

get weighted random item

I have, for example, this table

+-----------------+
| fruit  | weight |
+-----------------+
| apple  |   4    |
| orange |   2    |
| lemon  |   1    |
+-----------------+

I need to return a random fruit. But apple should be picked 4 times as frequent as Lemon and 2 times as frequent as orange.

In more general case it should be f(weight) times frequently.

What is a good general algorithm to implement this behavior?

Or maybe there are some ready gems on Ruby? :)

PS 
I've implemented current algorithm in Ruby https://github.com/fl00r/pickup

Question Protected by gnat
Replaced codeblock with <pre> to prevent syntax-highlightning.
Source Link

I have, for example, this table

+-----------------+
| fruit  | weight |
+-----------------+
| apple  |   4    |
| orange |   2    |
| lemon  |   1    |
+-----------------+

+-----------------+
| fruit  | weight |
+-----------------+
| apple  |   4    |
| orange |   2    |
| lemon  |   1    |
+-----------------+

I need to return random fruit. But apple should be picked 4 times frequently then lemon and 2 times frequently then orange.

In more general case it should be f(weight) times frequently.

What is general algorithm to implement this behavior?

Or maybe there is some ready gems on Ruby? :)

PS I've implemented current algorithm in Ruby https://github.com/fl00r/pickup

I have, for example, this table

+-----------------+
| fruit  | weight |
+-----------------+
| apple  |   4    |
| orange |   2    |
| lemon  |   1    |
+-----------------+

I need to return random fruit. But apple should be picked 4 times frequently then lemon and 2 times frequently then orange.

In more general case it should be f(weight) times frequently.

What is general algorithm to implement this behavior?

Or maybe there is some ready gems on Ruby? :)

PS I've implemented current algorithm in Ruby https://github.com/fl00r/pickup

I have, for example, this table


+-----------------+
| fruit  | weight |
+-----------------+
| apple  |   4    |
| orange |   2    |
| lemon  |   1    |
+-----------------+

I need to return random fruit. But apple should be picked 4 times frequently then lemon and 2 times frequently then orange.

In more general case it should be f(weight) times frequently.

What is general algorithm to implement this behavior?

Or maybe there is some ready gems on Ruby? :)

PS I've implemented current algorithm in Ruby https://github.com/fl00r/pickup

edited tags
Link
Dynamic
  • 5.8k
  • 9
  • 48
  • 74
added 8 characters in body
Source Link
fl00r
  • 701
  • 1
  • 6
  • 8
Loading
added 78 characters in body
Source Link
fl00r
  • 701
  • 1
  • 6
  • 8
Loading
Tweeted twitter.com/#!/StackProgrammer/status/207429889320685568
Source Link
fl00r
  • 701
  • 1
  • 6
  • 8
Loading