There are known rand7 () function returns a random natural number of 17 to make use of this rand7 () configured rand10 () random 1 - 10

This is a study of divergent thinking interview questions, the most important thing is the idea:
1. Assuming rand7 () function is a random natural number from 1 to 7, then the use of random natural number from 1 to 6 1/2 probability we can get parity
2. using rand7 () function is a random natural number of 1 to 5, it is even before the +5 output, or output directly

Here is the code for problem-solving answers:

using System;

namespace RandomAPP
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(new Random().Random10());
                System.Threading.Thread.Sleep(1000);
            }
            Console.ReadKey();

        }
    }

    class Random
    {
        /// <summary>
        ///  1到7随机自然数
        /// </summary>
        /// <returns>返回1-7随机自然数</returns>
        public int Random7()
        {
            return new System.Random().Next(1, 7);
        }

        /// <summary>
        ///  1到10随机自然数
        /// </summary>
        /// <returns>返回1-10随机自然数</returns>
        public int Random10()
        {
            int i = 0;
            while (true)
            {
                i = Random7();
                if (i != 7)
                {
                    break;
                }
            }

            System.Threading.Thread.Sleep(1000);

            int j = 0;
            while (true)
            {
                j = Random7();
                if (j < 6)
                {
                    break;
                }
            }

            if (i % 2 == 0)
            {
                return j + 5;
            }
            else
            {
                return j;
            }




        }




    }


}

Guess you like

Origin www.cnblogs.com/ButterflyEffect/p/12002516.html