简体   繁体   English

将数组中的一系列数字映射到统一随机 1 到 100

[英]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 之间的答案。

像你这样的声音正在尝试的任何地方产生随机数从1到100,您可以检查出的C ++ rand()函数是如何工作在这里,或Java的Math.random()函数在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM