1.系统随机数
主要原理便是依靠两个随机列'a','b'
通过@r选择器发生随机加和,最后的到如下:加和次数 | 第1次 | 第2次 | 第3次 | 第4次 | 第5次 | 第6次 | ... | 第n次 | a | 1 | 2 | - | - | 16 | - | ... | r(2n-1) | b | - | - | 4 | 8 | - | 32 | ... | r(2n-1) |
其中r(n)代表n是否存在(概率0.5)
最后将'a','b'两个随机列求和
借助@e[c=1]选择器舍弃一个随机列,另外一个随机列的值即为系统随机数。
模块:
- #随机列生成
- summon minecraft:area_effect_cloud ~ ~ ~ {Tags:[m_random],Duration:1}
- summon minecraft:area_effect_cloud ~ ~ ~ {Tags:[m_random],Duration:1}
- #随机发生器
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 1
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 2
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 4
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 8
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 16
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 32
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 64
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 128
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 256
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 512
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 1024
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 2048
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 4096
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 8192
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 16384
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 32768
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 65536
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 131072
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 262144
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 524288
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 1048576
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 2097152
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 4194304
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 8388608
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 16777216
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 33554432
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 67108864
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 134217728
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 268435456
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 536870912
- scoreboard players add @r[type=minecraft:area_effect_cloud,tag=m_random,r=0] random_min 1073741824
- #得到值
- scoreboard players operation @s random = @e[type=minecraft:area_effect_cloud,tag=m_random,r=0,c=1] random_min
复制代码 系统随机数范围为0~2147483647(minecraft计分板最多储存十位)。最终系统随机数储存在了random中
2.范围处理
- #获取范围
- scoreboard players operation @s random_max -= @s random_min
- scoreboard players add @s random_max 1
复制代码 获取随机的范围(max=max-min+1),此时max储存了随机范围
- scoreboard players operation @s random %= @s random_max
- scoreboard players operation @s random += @s random_min
复制代码 将random(系统随机数)与max(随即范围长度)求余,最终结果∈[0,随机范围)
然后用min(最小随机值)+最终结果,得到在[min.max]中的随机数
- #复位random_max
- scoreboard players operation @s random_max += @s random_min
- scoreboard players remove @s random_max 1
复制代码 用获取范围的逆算法来复位max
- #销毁随机列
- kill @e[type=minecraft:area_effect_cloud,tag=m_random,r=0]
复制代码 |