rand10 using rand7 (C#)
87
Aug 28, 2020
Aug 28, 2020

Here is my take on this..i am sure this can be optimized
to make equal distribution of numbers in the range 1 to 10, using 1 to 7 (rand7), we should call rand7 such that its total is a multiple of 10 which in this case is 70. so if we call rand7 10 times and then divide the summation by 10 and return the remainder, it would be equally distributed.

  public int Rand10() {
       var rnd = Rand7() + Rand7() + Rand7() + Rand7() + Rand7()
           + Rand7() + Rand7() + Rand7() + Rand7() + Rand7();
      
        return rnd%10 == 0 ? 10 : rnd%10;
    }
Comments (2)