gpt4 book ai didi

c# - 如何在数组中生成一个多于其他随机数的随机数?

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:56 24 4
gpt4 key购买 nike

我有一个数字列表,例如:{1,2,3,4,5,6}

我想随机生成这些数字,我是这样做的:

void Update(){
float ran = Random.Range(1,6);
print(ran);
}

如何生成或打印比其他数字多 3 的数字?

最佳答案

如果您想要倾斜分布,您可以将生成的值映射到所需的分布

// all 1..6 are equal with exception of 3 which appears more frequently 
// 1..2, 4..6 - 10% each (1 occurence per 10 items)
// 3 - 50% (5 occurences per 10 items)
private static int[] map = new int[1, 2, 3, 4, 5, 6, 3, 3, 3, 3];

...

void Update{
float ran = map[Random.Range(map.Length)];
print(ran);
}

关于c# - 如何在数组中生成一个多于其他随机数的随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39831417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com