简体   繁体   中英

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 }

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

please help

You could multiply the numbers on the dice by 1, 6, 6^2, 6^3, 6^4 respectively. Then add them up and take the last two digits of the result (that is, do a % 100 at the end).

Edit (thanks pjs)

As a last step, add 1, so that you get an answer between 1 and 100, not between 0 and 99.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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