给定能随机生成整数1到5的函数,写出能随机生成整数1到7的函数

  1.  只要我们可以从 n 个数中随机选出 1 到 n 个数,反复进行这种运算,直到剩下最后一个数即可。 
  2.         我们可以调用 n 次给定函数,生成 n 个 1 到 5 之间的随机数,选取最大数所在位置即可满足以上要求。 
  3.         例如 
  4.         初始的 7 个数 [1,2,3,4,5,6,7]. 
  5.         7 个 1 到 5 的随机数 [5, 3,1,4,2,5,5] 
  6.         那么我们保留下[1,6,7], 
  7.         3 个1 到 5 的随机数[2,4,1] 
  8.         那么我们保留下[6] 
  9.         6 就是我们这次生成的随机数。 
  10. http://bylijinnan.iteye.com/blog/1460963
    1.   /* solution 2: 
    2.      * return 1~7 randomly 
    3.      */  
    4.     public int rand7(){  
    5.         List<Integer> list=new ArrayList<Integer>();  
    6.         for(int i=1;i<=7;i++){  
    7.             list.add(i);  
    8.         }  
    9.         return rand7Help(list);  
    10.     }  
    11.       
    12.     public int rand7Help(List<Integer> list){  
    13.         if(list==null || list.size()==0){  
    14.             return -1;  
    15.         }  
    16.         if(list.size()==1){  
    17.             return list.get(0);  
    18.         }  
    19.           
    20.         int n=list.size();  
    21.         int[] nRand5=new int[n];  
    22.         int max=0;  
    23.         for(int i=0;i<n;i++){  
    24.             int x=rand5();  
    25.             if(x>max){  
    26.                 max=x;  
    27.             }  
    28.             nRand5[i]=x;  
    29.         }  
    30.           
    31.         List<Integer> newList=new ArrayList<Integer>();  
    32.         for(int i=0;i<n;i++){  
    33.             if(nRand5[i]==max){  
    34.                 newList.add(list.get(i));  
    35.             }  
    36.         }  
    37.         return rand7Help(newList);  
    38.     }  
    39.     public int rand5(){  
    40.         return new Random().nextInt(5)+1;  
    41.     }  
    42.       
    43. }  
    解法二:

    假设rand5能随机生成1~5的5个数(均等概率),利用rand5生成rand7() 1~7(均等概率)

    1. 利用rand5求出rand2(),当rand5生成的数大于2时,一直循环,直到它生成的数为1或者2,且生成1和2的概率均为1/2,因为生成1和生成2的概率是相等的。

    2. 利用rand2生成0和1(减1即可),连续使用三次,共有8种可能(二进制表示):000 001 010 011 100 101 110 111,当生成的数为000时,重新计算,这样就可以得到1~7范围内的数,且概率是均等的。

    [cpp]  view plain copy
    1. #include <iostream>  
    2. #include <math.h>  
    3.   
    4. using namespace std;  
    5.   
    6. int rand5()  
    7. {  
    8.     return rand()%5+1;  
    9. }  
    10.   
    11. int rand2()  
    12. {  
    13.     int temp;  
    14.     do   
    15.     {  
    16.         temp = rand5();  
    17.     } while (temp > 2);  
    18.   
    19.     return temp;  
    20. }  
    21.   
    22. int rand7()  
    23. {  
    24.     int temp;  
    25.     do   
    26.     {  
    27.         temp = (rand2()-1)<<2;  
    28.         temp += (rand2()-1)<<1;  
    29.         temp += rand2()-1;  
    30.     } while (temp == 0);  
    31.   
    32.     return temp;  
    33. }  
    34.   
    35. int main(intchar **)  
    36. {  
    37.     int num;  
    38.     cout << "输入总生成数:";  
    39.     cin >> num;  
    40.   
    41.     int c1, c2, c3, c4, c5, c6, c7;  
    42.     c1 = c2 = c3 = c4 = c5 = c6 = c7 = 0;  
    43.     for (int i = 0; i < num; i++)  
    44.     {  
    45.         int temp = rand7();  
    46.         switch (temp)  
    47.         {  
    48.         case 1:  
    49.             c1++;  
    50.             break;  
    51.         case 2:  
    52.             c2++;  
    53.             break;  
    54.         case 3:  
    55.             c3++;  
    56.             break;  
    57.         case 4:  
    58.             c4++;  
    59.             break;  
    60.         case 5:  
    61.             c5++;  
    62.             break;  
    63.         case 6:  
    64.             c6++;  
    65.             break;  
    66.         case 7:  
    67.             c7++;  
    68.         }  
    69.   
    70.         //cout << temp << "\t";  
    71.     }  
    72.   
    73.     cout << endl;  
    74.     cout << "生成1的个数占总生成数的:" << (double)c1/num*100 << "%" << endl;  
    75.     cout << "生成2的个数占总生成数的:" << (double)c2/num*100 << "%" << endl;  
    76.     cout << "生成3的个数占总生成数的:" << (double)c3/num*100 << "%" << endl;  
    77.     cout << "生成4的个数占总生成数的:" << (double)c4/num*100 << "%" << endl;  
    78.     cout << "生成5的个数占总生成数的:" << (double)c5/num*100 << "%" << endl;  
    79.     cout << "生成6的个数占总生成数的:" << (double)c6/num*100 << "%" << endl;  
    80.     cout << "生成7的个数占总生成数的:" << (double)c7/num*100 << "%" << endl;  
    81.   
    82.     return 0;  
    83. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值