设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2540|回复: 3
打印 上一主题 下一主题

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

[复制链接]

Lv3.寻梦者

唯一的信徒

梦石
0
星屑
1665
在线时间
1357 小时
注册时间
2013-1-29
帖子
1637
跳转到指定楼层
1
发表于 2015-8-26 10:14:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
其实说是拓展实际上就增加了几个方法,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
『我只是一个正在潜心修炼的渣乐师罢了』
Dear Time\(^o^)/~


假如上面的图片挂了的话麻烦各位去发个帖 @ 一下 orzFly 让他修复 deartime

Lv4.逐梦者 (版主)

梦石
0
星屑
9497
在线时间
5073 小时
注册时间
2013-6-21
帖子
3580

开拓者贵宾剧作品鉴家

2
发表于 2015-8-26 10:31:26 | 只看该作者
附上友情链接:
https://rpg.blue/forum.php?mod=viewthread&tid=375705
本帖采用Box-Muller方法实现正态分布,而链接中的帖子叙述了此方法的原理。
回复 支持 反对

使用道具 举报

Lv4.逐梦者

梦石
0
星屑
9275
在线时间
2504 小时
注册时间
2011-5-20
帖子
15389

开拓者

3
发表于 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
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
1759
在线时间
2524 小时
注册时间
2010-10-12
帖子
1454

开拓者

4
发表于 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

回复 支持 反对

使用道具 举报

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

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-5 04:38

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表