[英]to map a range of numbers in an array to uniform random 1 to 100
I have 5 dice and I roll each of them once: say outcome is dice[] = { 1, 1, 1, 1, 1 }我有 5 个骰子,每个骰子都掷一次:说结果是骰子 [] = { 1, 1, 1, 1, 1 }
this dice[] array is the input to a function say这个骰子[]数组是一个函数的输入说
int random (dice) {
//do some thing with the dice values { 1, 1, 1, 1, 1 } ( as input from user )
//return a uniform random number between 1 to 100
}
Rule: only source of randomness is the values in the dice array.规则:唯一的随机源是骰子数组中的值。 How do I may each of the values to a number in 1 to 100 so that the resulting values are almost uniform generation
我如何将每个值转换为 1 到 100 之间的数字,以便生成的值几乎是统一的
please help请帮忙
You could multiply the numbers on the dice by 1, 6, 6^2, 6^3, 6^4 respectively.您可以将骰子上的数字分别乘以 1、6、6^2、6^3、6^4。 Then add them up and take the last two digits of the result (that is, do a % 100 at the end).
然后将它们相加并取结果的最后两位数(即,在最后做一个 % 100)。
Edit (thanks pjs)编辑(感谢 pjs)
As a last step, add 1, so that you get an answer between 1 and 100, not between 0 and 99.最后一步,加 1,这样你就能得到介于 1 和 100 之间的答案,而不是介于 0 和 99 之间的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.