`
fireflyman
  • 浏览: 113378 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
从数组取随机数 ruby Ruby实现,从数组中随机取数!
class Array
  def random(weights)
    weights ||= [1.0] * length
    total = weights.inject(0.0) {|sum, w| sum + w}
    point = rand * total

    zip(weights).sort{|a, b| a[1] <=> b[1]}.each do |element, weight|
      return element if weight >= point
      point -= weight
    end
  end
end
eventmachine helloworld ruby hello EventMachine
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
EM.run do
  EM::PeriodicTimer.new(1) do
    puts Time.now
  end
end

Global site tag (gtag.js) - Google Analytics