Skip to content

Helpers #10

@MatheusRich

Description

@MatheusRich
  • Weighted sample
class Hash
  def wsample
    max_by { |_, w|
      next -Float::INFINITY if w == 0

      rand**(1.0 / w)
    }&.first # first to return just the key
  end

  def wsample_strict
    total_probability = values.sum
    raise ArgumentError, "Weights must add up to 1, but they add up to #{total_probability}." if total_probability != 1

    wsample
  end
end
  • bresenham's line
  • @canvas.draw_line/draw_rect_outline

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions