Module: Halton

Defined in:
lib/halton.rb

Overview

The Halton module provides methods for generating Halton sequences, a deterministic low discrepancy sequence that appears to be random. The uniform distribution and repeatability makes the sequence ideal for choosing sample points or placing objects in 2D or 3D space.

grid = 10.times.map {10.times.map {"."}}
Halton.each(2, 3).take(26).zip("A".."Z") do |(x, y), c|
  grid[y * 10][x * 10] = c
end
grid.each {|row| puts row.join(" ")}

Outputs:

. . R . . I . . . .
. L . . . . U C . .
X . . F . . . . . O
. . . J . A . . . .
. D . . . . M S . .
P . . . V . . . G .
. . B . . Y . . . .
. T . . . . E . K .
H . . . N . . . . W
. . . Z . Q . . . .

Defined Under Namespace

Classes: Sequence

Class Method Summary collapse

Class Method Details

.each(base, *bases, &block) ⇒ Object

:call-seq: Halton.each(base) {|n| … } Halton.each(base_x, base_y) {|x, y| … } Halton.each(base_x, base_y, base_z) {|x, y, z| … } Halton.each(*bases) {|*n| … } Halton.each(*bases) -> Enumerator

Implements the fast generation of Halton sequences. The method of generation is adapted from “Fast, portable, and reliable algorithm for the calculation of Halton numbers” by Miroslav Kolář and Seamus F. O’Shea.

The numbers yielded will be in the range > 0 and < 1.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/halton.rb', line 80

def self.each(base, *bases, &block)
  case bases.length
  when 0
    each_one(base, &block)
  when 1
    each_pair(base, bases.first, &block)
  when 2
    each_triple(base, bases.first, bases.last, &block)
  else
    each_many(*bases.unshift(base), &block)
  end
end