Project1

标题: 【辅助】随机数拓展 [打印本页]

作者: LBQ    时间: 2015-8-26 10:14
标题: 【辅助】随机数拓展
其实说是拓展实际上就增加了几个方法,VA里面可以带战斗公式里面用。

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

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

RUBY 代码复制
  1. #encoding:utf-8
  2. #==============================================================================
  3. # ■ Rand Formulas
  4. #------------------------------------------------------------------------------
  5. #  Provides additional random methods, mainly for battle formulas
  6. #==============================================================================
  7. # ● Rd.gauss(mean : Number, stddev : Number)
  8. #    生成高斯分布的一个随机数(貌似是正态分布或者类似),第一个参数是平均值,第二个参数是方差
  9. #    Generates a random gaussian distributed number.
  10. #    Eg. Rd.gauss(1,0.1)
  11. #==============================================================================
  12. # ● Rd.percentage
  13. #    返回一个1~100的数值
  14. #    Returns a value between 1 and 100
  15. #==============================================================================
  16. # ● Rd.between(min : Number, max : Number)
  17. #    平均分布的随机数,位于min与max之间。
  18. #    Generates a equally distributed random number between min and max.
  19. #==============================================================================
  20. # ● Rd.betweenP(min : Number, max : Number)
  21. #    平均分布的随机数,位于min/100与max/100之间
  22. #    Generates a equally distributed random number between min/100 and max/100
  23. #==============================================================================
  24.  
  25. =begin
  26. This is free and unencumbered software released into the public domain.
  27.  
  28. Anyone is free to copy, modify, publish, use, compile, sell, or
  29. distribute this software, either in source code form or as a compiled
  30. binary, for any purpose, commercial or non-commercial, and by any
  31. means.
  32.  
  33. In jurisdictions that recognize copyright laws, the author or authors
  34. of this software dedicate any and all copyright interest in the
  35. software to the public domain. We make this dedication for the benefit
  36. of the public at large and to the detriment of our heirs and
  37. successors. We intend this dedication to be an overt act of
  38. relinquishment in perpetuity of all present and future rights to this
  39. software under copyright law.
  40.  
  41. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  42. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  43. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  44. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  45. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  46. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  47. OTHER DEALINGS IN THE SOFTWARE.
  48.  
  49. For more information, please refer to <[url]http://unlicense.org/[/url]>
  50. =end
  51.  
  52.  
  53. class RandomGaussian
  54.   def initialize(mean, stddev, rand_helper = lambda { Kernel.rand })
  55.     @rand_helper = rand_helper
  56.     @mean = mean
  57.     @stddev = stddev
  58.     @valid = false
  59.     @next = 0
  60.   end
  61.  
  62.   def rand
  63.     if @valid then
  64.       @valid = false
  65.       return @next
  66.     else
  67.       @valid = true
  68.       x, y = self.class.gaussian(@mean, @stddev, @rand_helper)
  69.       @next = y
  70.       return x
  71.     end
  72.   end
  73.  
  74.   private
  75.   def self.gaussian(mean, stddev, rand)
  76.     theta = 2 * Math::PI * rand.call
  77.     rho = Math.sqrt(-2 * Math.log(1 - rand.call))
  78.     scale = stddev * rho
  79.     x = mean + scale * Math.cos(theta)
  80.     y = mean + scale * Math.sin(theta)
  81.     return x, y
  82.   end
  83. end
  84.  
  85. #[url]http://stackoverflow.com/questions/5825680/code-to-generate-gaussian-normally-distributed-random-numbers-in-ruby[/url]
  86. module Rd
  87.   class << self
  88.     # Number -> Number -> Number
  89.     def gauss mean, stddev
  90.       g = RandomGaussian.new(mean, stddev)
  91.       return g.rand
  92.     end
  93.  
  94.     # Unit -> Number
  95.     def percentage
  96.       return Random.new.rand(1..100)
  97.     end
  98.  
  99.     # Number -> Number -> Number
  100.     def between a, b
  101.       return Random.new.rand(a..b)
  102.     end
  103.  
  104.     # Number -> Number -> Number
  105.     def betweenP a, b
  106.       return between(a,b) / 100.0
  107.     end
  108.   end
  109. end

作者: RyanBern    时间: 2015-8-26 10:31
附上友情链接:
https://rpg.blue/forum.php?mod=viewthread&tid=375705
本帖采用Box-Muller方法实现正态分布,而链接中的帖子叙述了此方法的原理。
作者: chd114    时间: 2015-8-27 11:37
为啥你发的脚本却用英文做注释呢···
作者: 刺夜之枪    时间: 2015-8-27 12:14
如果我游戏用很多搞死的话,会不会卡呢




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1