#encoding:utf-8
#==============================================================================
# ■ Rand Formulas
#------------------------------------------------------------------------------
# Provides additional random methods, mainly for battle formulas
#==============================================================================
# ● Rd.gauss(mean : Number, stddev : Number)
# 生成高斯分布的一个随机数(貌似是正态分布或者类似),第一个参数是平均值,第二个参数是方差
# Generates a random gaussian distributed number.
# Eg. Rd.gauss(1,0.1)
#==============================================================================
# ● Rd.percentage
# 返回一个1~100的数值
# Returns a value between 1 and 100
#==============================================================================
# ● Rd.between(min : Number, max : Number)
# 平均分布的随机数,位于min与max之间。
# Generates a equally distributed random number between min and max.
#==============================================================================
# ● Rd.betweenP(min : Number, max : Number)
# 平均分布的随机数,位于min/100与max/100之间
# Generates a equally distributed random number between min/100 and max/100
#==============================================================================
=begin
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <[url]http://unlicense.org/[/url]>
=end
class RandomGaussian
def initialize(mean, stddev, rand_helper = lambda { Kernel.rand })
@rand_helper = rand_helper
@mean = mean
@stddev = stddev
@valid = false
@next = 0
end
def rand
if @valid then
@valid = false
return @next
else
@valid = true
x, y = self.class.gaussian(@mean, @stddev, @rand_helper)
@next = y
return x
end
end
private
def self.gaussian(mean, stddev, rand)
theta = 2 * Math::PI * rand.call
rho = Math.sqrt(-2 * Math.log(1 - rand.call))
scale = stddev * rho
x = mean + scale * Math.cos(theta)
y = mean + scale * Math.sin(theta)
return x, y
end
end
#[url]http://stackoverflow.com/questions/5825680/code-to-generate-gaussian-normally-distributed-random-numbers-in-ruby[/url]
module Rd
class << self
# Number -> Number -> Number
def gauss mean, stddev
g = RandomGaussian.new(mean, stddev)
return g.rand
end
# Unit -> Number
def percentage
return Random.new.rand(1..100)
end
# Number -> Number -> Number
def between a, b
return Random.new.rand(a..b)
end
# Number -> Number -> Number
def betweenP a, b
return between(a,b) / 100.0
end
end
end