如何生成不重复随机数
具体情况:
有36个目标,从中可以随机抽取1~36个(也就是每次生成的随机数个数可以自定义),但抽取不重复
具体情况:
有36个目标,从中可以随机抽取1~36个(也就是每次生成的随机数个数可以自定义),但抽取不重复
建议一个可能可行的方案:
summon一个实体
取其uuid并存入计分板
对计分板的那个数取模(%36)
只要那个被召唤的实体不趋势,应该就不可能有重复;就算实体趋势,抽到相同uuid的可能性也是极小的
summon一个实体
取其uuid并存入计分板
对计分板的那个数取模(%36)
只要那个被召唤的实体不趋势,应该就不可能有重复;就算实体趋势,抽到相同uuid的可能性也是极小的
不是有计分板吗,用计分板自带的随机分数指令,每随机一次就记录一次数字。比如随机到了26,就把26记录下来,由于随机分数(范围可控)可能重复,那么在计分板再次随机到26时就重新生成随机数,做到不重复
cvbyisme 发表于 2023-7-17 21:08
不是有计分板吗,用计分板自带的随机分数指令,每随机一次就记录一次数字。比如随机到了26,就把26记录下来 ...
这样会很麻烦,而且判定起来不容易,计算量也大
简单的洗牌:
generic:list/shuffle/do
复制代码generic:list/shuffle/loop复制代码generic:list/shuffle/zzz/0复制代码generic:list/shuffle/result
复制代码
generic:list/shuffle/do
- # 用法:
- # 先执行 data modify storage generic:main ListInput0 set from <待洗牌的列表>
- # 再执行 function generic:list/shuffle/do
- # 输出会放在generic:main ListOutput0当中
- execute store result score #list.temp.0 loy.value run data get storage generic:main ListInput0
- execute if score #list.temp.0 loy.value matches 1.. positioned 0.0 1.0 0.0 run function generic:list/shuffle/loop
- data modify storage generic:main ListOutput0 set value []
- execute positioned 0.0 1.0 0.0 as @e[type=marker,tag=entity.temp,distance=...1,sort=random] run function generic:list/shuffle/result
- execute summon minecraft:marker run function generic:list/shuffle/zzz/0
- scoreboard players remove #list.temp.0 loy.value 1
- execute if score #list.temp.0 loy.value matches 1.. run function generic:list/shuffle/loop
- data modify entity @s data.Element set from storage generic:main ListInput0[0]
- data remove storage generic:main ListInput0[0]
- tag @s add entity.temp
- tag @s add loy.obj
- data modify storage generic:main ListOutput0 append from entity @s data.Element
- kill @s