Class: Splib::IdealHumanRandomIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/splib/HumanIdealRandomIterator.rb

Overview

IdealHumanRandomIterator - select “random” members of a population, favoring those least-recently selected, to appease silly humans who hate repeats

Abstract: given a decently-sized set of items (say, 100 famous quotes), an average persons’s idea of N “random” entries is not actually random. people don’t want items to appear twice in a row, or too frequently (even though true randomness means this is just as likely as any other order).

instead, design a scheme whereby LRU items are weighted more heavily, to “encourage” subsequent selections to not repeat.

Author: Ryan “pizza_” Flynn

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ IdealHumanRandomIterator

Returns a new instance of IdealHumanRandomIterator.

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/splib/HumanIdealRandomIterator.rb', line 19

def initialize(list)
    raise ArgumentError.new("Array type required") unless list.is_a?(Array)
    @items = list
end

Class Method Details

.nexti(len) ⇒ Object

Given length L, generate a random number in the range [0,len-1), heavily weighted towards the low end.



26
27
28
29
30
# File 'lib/splib/HumanIdealRandomIterator.rb', line 26

def self.nexti(len)
    len += 1 if len % 2 == 1
    index = len > 2 ? rand(len/2) : 0
    return index
end

Instance Method Details

#nextObject

return a psuedo-random member of items. subsequent calls should never return the same item.



34
35
36
37
38
# File 'lib/splib/HumanIdealRandomIterator.rb', line 34

def next()
    index = IdealHumanRandomIterator.nexti(@items.length)
    @items.push @items.delete_at(index)
    return @items.last
end