Class: Fakir::Random

Inherits:
Object
  • Object
show all
Defined in:
lib/fakir/random.rb

Overview

Returns not-quite-random numbers: if window is nil, then an instance will never return the same number. If window is an integer N, then a random number will not be returned within N invocations of rand. Thus with window == 2, the same number won’t be returned twice in a row. If number is 5, then the same number will not be returned within 5 subsequent invocations of rand times than max. If number is 1, then the number can be repeated subsequently.

Instance Method Summary collapse

Constructor Details

#initialize(max, window = nil) ⇒ Random

Returns a new instance of Random.



11
12
13
14
15
16
# File 'lib/fakir/random.rb', line 11

def initialize max, window = nil
  raise "window #{window} should be less than maximum #{max}" if window && window >= max
  @used = Array.new
  @max = max
  @window = window
end

Instance Method Details

#randObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fakir/random.rb', line 18

def rand
  iters = 0
  while iters < @max ** 4
    num = Kernel::rand @max
    if @used.include?(num)
      iters += 1
    else
      @used << num
      if @window
        @used = @used.drop [ @used.length - @window + 1, 0 ].max
      end
      return num
    end
  end
  nil
end