一道腾讯面试题
2011-09-27 12:10
218 查看
/** * 已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10。 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Rand10 rand10 = Rand10.GetInstance(); long total = 9999999; //记录1~10的数产生的次数 int[] numArray = new int[10]; for (long i = 0; i < total; i++) { int randomNumber = rand10.Next(); numArray[randomNumber - 1]++; } //打印产生各数的概率 for (int i = 0; i < numArray.Length; i++) { Console.WriteLine(string.Format("产生{0}的概率是:{1:0.00000}", i + 1, (Double)numArray[i] / total)); } Console.ReadLine(); } } //1~7的随机数产生类 public class Rand7 { private static Rand7 _rand7; private readonly Random _random = new Random(); private Rand7() { } public static Rand7 GetInstance() { if(_rand7==null) { _rand7 = new Rand7(); } return _rand7; } //获得随机数 public int Next() { return _random.Next(1, 8); } } //1~10的随机数产生类 public class Rand10 { private static Rand10 rand10; private Rand7 _rand7 = Rand7.GetInstance(); private Rand10() { } public static Rand10 GetInstance() { if(rand10==null) { rand10 = new Rand10(); } return rand10; } //获得随机数 public int Next() { int num; //均匀产生1、 2 、3、4、5 while (true) { num = _rand7.Next(); if (num <= 5) break; } while (true) { int n = _rand7.Next(); if (n == 4) continue; //n大于4的数字有5、6、7,因为是由Rand7产生的,所以概率均匀 if (n > 4) //因为num只可取值1、2、3、4、5并且取值概率均匀,num*2可得2、4、6、8、10也概率均匀 num *= 2; //n小于4的数字有1、2、3,因为是由Rand7产生的,所以概率均匀 else //因为num只可取值1、2、3、4、5并且取值概率均匀,num*2-1可得1、3、5、7、9也概率均匀 num = num * 2 - 1; break; } return num; } } }
相关文章推荐
- 一道腾讯链表面试题
- 一道腾讯面试题
- 由一道腾讯面试题引发对于关联数组的思考
- 腾讯的一道javascript面试题
- 2016/12/30其实一直想写的一道腾讯面试题
- 腾讯的一道面试题
- 一道腾讯面试题:根据上排给出十个数,在其下排填出对应的十个数
- 一道腾讯的面试题
- 一道腾讯面试题:根据上排给出十个数,在其下排填出对应的十个数
- 腾讯的一道面试题
- 一道腾讯面试题的思考:到底谁会赢?
- 一道腾讯面试题
- 腾讯的一道面试题
- 腾讯的一道面试题—不用除法求数字乘积
- 腾讯的一道面试题
- 腾讯的一道Script面试题
- 腾讯一道找中位数的面试题
- 小伙伴们 ,今天做了腾讯的一道变量提升的面试题,试一试看你能否进腾讯!
- 一道腾讯的面试题,关于a和&a
- 一道腾讯的面试题,关于a和&a