🎲 随机(Random)#
#bs.random:help
包含许多用于不同分布和噪声模式的随机函数。
创造力是将秩序引入自然的随机性中的能力。
—埃里克·霍弗(Eric Hoffer)
🔧 函数#
你可以在下方找到此模块中的所有可用函数。
随机选择#
- #bs.random:choice
从列表中随机选择一个值。
- 输入:
命令存储
bs:in random.choice.options: 要从中选择的值列表。- 输出:
命令存储
bs:out random.choice: 随机选择的值。返回值:所选值的索引。
示例:从列表中随机选择一个水果:
# Populate list with values
data modify storage bs:in random.choice.options set value ["Apple", "Banana", "Strawberry", "Blueberry", "Mango", "Watermelon"]
# Randomly select one
function #bs.random:choice
# Display the result
tellraw @a [{"text":"Value: ","color":"dark_gray"},{"nbt":"random.choice","storage":"bs:out","color":"gold"}]
- #bs.random:weighted_choice
基于指定权重从列表中随机选择一个值。
- 输入:
命令存储
bs:in random.weighted_choice.options: 要从中选择的值列表。命令存储
bs:in random.weighted_choice.weights: 每个值对应的权重。- 输出:
命令存储
bs:out random.weighted_choice: 基于权重随机选择的值。返回值:所选值的索引。
示例:基于权重从列表中随机选择一个水果:
# Populate list with values
data modify storage bs:in random.weighted_choice.options set value ["Apple", "Banana", "Strawberry"]
# Populate list with weights
data modify storage bs:in random.weighted_choice.weights set value [5, 3, 2]
# Randomly select one based on weights
function #bs.random:weighted_choice
# Display the result
tellraw @a [{"text":"Value: ","color":"dark_gray"},{"nbt":"random.weighted_choice","storage":"bs:out","color":"gold"}]
制作人员:Aksiome、SBtree
随机分布#
- #bs.random:uniform {min:<min>,max:<max>}
生成一个在
min和max之间均匀分布的随机数。- 输入:
函数宏:
参数
min: 下限。
max: 上限。
- 输出:
返回值 | 分数
$random.uniform bs.out:[min,max] 范围内的随机整数。
示例:生成1到100之间的随机数:
# Generate random number
function #bs.random:uniform {min:1,max:100}
# Display the result
tellraw @a [{"text": "Number: ", "color": "dark_gray"},{"score":{"name":"$random.uniform", "objective": "bs.out"}, "color": "gold"}]
What is a Uniform Distribution?
每个结果都有相同的发生概率。就像掷一个公平的骰子,每个数字(1到6)的概率相同。
- #bs.random:binomial {trials:<trials>,probability:<probability>}
使用指定的
trials(试验次数)和probability(概率)生成符合二项分布的随机数。- 输入:
函数宏:
参数
trials: 伯努利试验的次数。最大数量限制为1000。
probability: 每个伯努利试验的成功概率,范围在[0,1]。
- 输出:
返回值 | 分数
$random.binomial bs.out:[0,trials] 范围内的随机整数。
示例:生成一个有10次试验和20%成功几率的随机数:
# Generate random number
function #bs.random:binomial {trials:10,probability:0.2}
# Display the result
tellraw @a [{"text": "Number: ", "color": "dark_gray"},{"score":{"name":"$random.binomial", "objective": "bs.out"}, "color": "gold"}]
What is a Binomial Distribution?
这个分布用于描述在固定次数的独立试验中(每次试验只有成功或失败两种结果,且成功概率相同),恰好发生“成功”的概率。举个例子,可以用这个分布模拟抛硬币10次,出现正面的次数。
- #bs.random:geometric {probability:<probability>}
使用给定的
probability(概率)生成符合几何分布的随机数。- 输入:
函数宏:
参数
probability: 每次试验的停止概率,范围在[0,1]。
- 输出:
返回值 | 分数
$random.geometric bs.out:[0, 1000] 范围内的随机整数。
示例:生成一个有2%停止几率的随机数:
# Generate random number
function #bs.random:geometric {probability:0.02}
# Display the result
tellraw @a [{"text": "Number: ", "color": "dark_gray"},{"score":{"name":"$random.geometric", "objective": "bs.out"}, "color": "gold"}]
What is a Geometric Distribution?
衡量在一系列独立重复试验中首次成功所需的试验次数。例如,掷骰子获得6点所需的次数。
- #bs.random:normal {mean:<mean>,spread:<spread>}
指定
mean(均值)和spread(标准差),生成一个符合正态(高斯)分布的随机数。- 输入:
函数宏:
参数
mean: 正态分布的均值(中心)。
spread: 标准差,用于控制随机数的分散程度。
- 输出:
返回值 | 分数
$random.uniform bs.out:在mean附近的随机整数。
示例:生成分散程度(标准差)为3,均值为10的正态分布随机数。
# Generate random number
function #bs.random:normal {mean:10,spread:3}
# Display the result
tellraw @a [{"text": "Number: ", "color": "dark_gray"},{"score":{"name":"$random.normal", "objective": "bs.out"}, "color": "gold"}]
What is a Normal Distribution?
正态分布,也被称为高斯分布。它产生的值聚集在均值周围,形成钟形概率曲线。这个分布广泛用于建模自然变化和测量误差。
- #bs.random:poisson {lambda:<lambda>}
使用期望值
lambda生成符合泊松分布的随机数。- 输入:
函数宏:
参数
lambda: 期望在[0,10]的值。
- 输出:
返回值 | 分数
$random.poisson bs.out:偏向lambda值的随机整数。
示例:生成一个偏向5的随机数:
# Generate random number
function #bs.random:poisson {lambda:5}
# Display the result
tellraw @a [{"text": "Number: ", "color": "dark_gray"},{"score":{"name":"$random.poisson", "objective": "bs.out"}, "color": "gold"}]
What is a Poisson Distribution?
泊松分布生成围绕平均值 λ (lambda) 聚集的值。结果倾向于集中在这个平均值附近,这使它非常适合模拟以固定速率发生的随机事件。
制作人员:Aksiome、SBtree
噪声算法#
- #bs.random:simplex_noise_2d
计算给定坐标处的单纯形噪声值。单纯形噪声是一种梯度噪声,类似于柏林噪声,用于程序生成技术或者纹理。
- 输入:
分数
$random.simplex_noise_2d.seed bs.in:用于生成的种子。分数
$random.simplex_noise_2d.[x,y] bs.in:坐标,缩放1000倍(例如,对于单元格大小为16,乘以1000/16)。- 输出:
返回值 | 分数
$random.simplex_noise_2d bs.out:范围在 [-1000,1000] 的噪声值。
示例:生成一个随机噪声值:
# Generate random noise value
scoreboard players set $random.simplex_noise_2d.x bs.in 15589
scoreboard players set $random.simplex_noise_2d.y bs.in 812
execute store result score $random.simplex_noise_2d.seed bs.in run random value -1000000000..1000000000
function #bs.random:simplex_noise_2d
- #bs.random:fractal_noise_2d
通过在不同尺度上组合多个噪声层来生成分形噪声,常用于地形生成。
- 输入:
分数
$random.fractal_noise_2d.seed bs.in:用于生成的种子。分数
$random.fractal_noise_2d.[x,y] bs.in:坐标,缩放1000倍(例如,对于单元格大小为16,乘以1000/16)。分数
$random.fractal_noise_2d.octaves bs.in:要组合的噪声层数(更多倍频程 = 更多细节)。分数
$random.fractal_noise_2d.persistence bs.in:每个倍频程对总噪声的贡献,缩放1000倍。分数
$random.fractal_noise_2d.lacunarity bs.in:每个倍频程的频率增长,缩放1000倍。- 输出:
返回值 | 分数
$random.fractal_noise_2d bs.out:范围在 [-1000,1000] 的噪声值。
示例:生成一个随机噪声值:
# Generate random noise value
scoreboard players set $random.fractal_noise_2d.x bs.in 15589
scoreboard players set $random.fractal_noise_2d.y bs.in 812
scoreboard players set $random.fractal_noise_2d.octaves bs.in 4
scoreboard players set $random.fractal_noise_2d.persistence bs.in 500
scoreboard players set $random.fractal_noise_2d.lacunarity bs.in 2000
execute store result score $random.fractal_noise_2d.seed bs.in run random value -1000000000..1000000000
function #bs.random:fractal_noise_2d
制作人员:Aksiome、SBtree
噪声矩阵#
- #bs.random:white_noise_mat_1d {length:<length>,with:{}}
生成一维白噪声值数组。
- 输入:
函数宏:
参数
length: 要生成的数组长度。
with:
scale: 函数输出的缩放系数(默认:1)。
- 输出:
命令存储
bs:out random.white_noise_mat_1d: 0 到 {scale} 之间的值数组。
示例:生成4个随机值:
# Generate random noise
function #bs.random:white_noise_mat_1d {length:4,with:{}}
# Display the result
tellraw @a [{"text": "Noise: ", "color": "dark_gray"},{"nbt":"white_noise_mat_1d","storage":"bs:out", "color": "gold"}]
- #bs.random:white_noise_mat_2d {width:<width>,height:<height>,with:{}}
生成二维白噪声值数组。
- 输入:
函数宏:
参数
width: 要生成的数组的宽度。
width: 要生成的数组的高度。
with:
scale: 函数输出的缩放系数(默认:1)。
- 输出:
命令存储
bs:out random.white_noise_mat_2d: 0到{scale}之间的值的二维数组。
示例:生成4x4随机噪声图案:
# Generate random noise
function #bs.random:white_noise_mat_2d {width:4,height:4,with:{}}
# Display the result
tellraw @a [{"text": "Noise: ", "color": "dark_gray"},{"nbt":"white_noise_mat_2d","storage":"bs:out", "color": "gold"}]
- #bs.random:simplex_noise_mat_2d {width:<width>,height:<height>,with:{}}
生成大小为
width×height的二维单纯形噪声纹理。单纯形噪声是一种常用于纹理的梯度噪声。- 输入:
函数宏:
参数
width: 要生成的数组的宽度。
width: 要生成的数组的高度。
with:
size: 噪声晶格的大小(默认:16)。这个值越小,结果就越复杂。
seed: 噪声的种子,方便复现结果(默认:random)。
scale: 函数输出的缩放系数(默认:1)。
- 输出:
命令存储
bs:out random.simplex_noise_mat_2d:{-scale} 到 {scale} 之间的值的二维数组。
示例:生成16×16单纯形噪声图案:
# Generate random noise
function #bs.random:simplex_noise_mat_2d {width:16,height:16,with:{size:8}}
# Display the result
tellraw @a [{"text": "Noise: ", "color": "dark_gray"},{"nbt":"simplex_noise_mat_2d","storage":"bs:out", "color": "gold"}]
- #bs.random:fractal_noise_mat_2d {width:<width>,height:<height>,with:{}}
生成大小为
width×height的二维分形噪声纹理。分形噪声将多层噪声组合来创建更复杂的纹理。- 输入:
函数宏:
参数
width: 要生成的数组的宽度。
width: 要生成的数组的高度。
with:
size: 噪声晶格的大小(默认:16)。这个值越小,结果就越复杂。
seed: 噪声的种子,方便复现结果(默认:random)。
分数
$random.fractal_noise_2d.octaves bs.in:要组合的噪声层数(默认:4)。更多倍频程 = 更多细节。persistence: 每个倍频程的贡献(默认:0.5),更高的值细节更多。
lacunarity: 每个倍频程的频率增长(默认:2.0),值越高频率增长越快。
scale: 函数输出的缩放系数(默认:1)。
- 输出:
命令存储
bs:out random.fractal_noise_mat_2d:{-scale} 到 {scale} 之间的值的二维数组。
示例:生成16×16分形噪声图案:
# Generate random noise
function #bs.random:fractal_noise_mat_2d {width:16,height:16,with:{size:8}}
# Display the result
tellraw @a [{"text": "Noise: ", "color": "dark_gray"},{"nbt":"fractal_noise_mat_2d","storage":"bs:out", "color": "gold"}]



制作人员:Aksiome、SBtree
💬 这对你有帮助吗?
欢迎在下方留下你的问题和反馈!