查看: 3105|回复: 3

[通用发布] 【辅助】随机数拓展

[复制链接]

唯一的信徒

梦石
0
星屑
1697
在线时间
1359 小时
注册时间
2013-1-29
回帖
1537
发表于 2015-8-26 10:14:54 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
其实说是拓展实际上就增加了几个方法,VA里面可以带战斗公式里面用。

说明在脚本里面我认为说的够清楚了

然后这样你可以在VA的公式里面写:(a.atk * 4 - b.def * 2) * Rd.gauss(1,0.2)
这样就会生成正态分布的伤害

[pre lang="ruby"]#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 <http://unlicense.org/>
=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

#http://stackoverflow.com/questio ... dom-numbers-in-ruby
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



[/pre]
『我只是一个正在潜心修炼的渣乐师罢了』
Dear Time\(^o^)/~


假如上面的图片挂了的话麻烦各位去发个帖 @ 一下 orzFly 让他修复 deartime
梦石
0
星屑
9557
在线时间
5074 小时
注册时间
2013-6-21
回帖
3459

开拓者贵宾剧作品鉴家

发表于 2015-8-26 10:31:26 | 显示全部楼层
附上友情链接:
https://rpg.blue/forum.php?mod=viewthread&tid=375705
本帖采用Box-Muller方法实现正态分布,而链接中的帖子叙述了此方法的原理。
回复

使用道具 举报

梦石
0
星屑
9292
在线时间
2504 小时
注册时间
2011-5-20
回帖
14521

开拓者

发表于 2015-8-27 11:37:13 | 显示全部楼层
为啥你发的脚本却用英文做注释呢···

点评

······你还是用英文吧  发表于 2015-8-27 13:53
英文注释是一个好习惯。不过我这种英文渣还在用中文  发表于 2015-8-27 13:50
→w→  发表于 2015-8-27 11:55
LBQ
中英文介绍都有啊,又不是只有英文。用英文只是觉得万一哪天要发到国外用得着于是就顺手写了  发表于 2015-8-27 11:39
[img]http://service.t.sina.com.cn/widget/qmd/5339802982/c02e16bd/7.png
回复

使用道具 举报

梦石
0
星屑
1816
在线时间
2527 小时
注册时间
2010-10-12
回帖
1336

开拓者

发表于 2015-8-27 12:14:53 | 显示全部楼层
如果我游戏用很多搞死的话,会不会卡呢

点评

LBQ
每帧100次实测没问题60Fps  发表于 2015-8-27 18:37
每帧100次呢-w-?  发表于 2015-8-27 13:18
LBQ
1000000次高斯计算4.5秒这里,所以效率肯定不是问题,毕竟“很多”估计不会到每秒钟上千次....  发表于 2015-8-27 13:01

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部