真正随机数的产生

        今天小明和小红玩游戏,决定如果谁要是输了就要受到惩罚。可是怎么惩罚呢,惩罚的方式小明找了找,找到了这么几条:

1,自己主动激情舌吻10s以上

2,上下对卧,自己做10个俯卧撑

3,脱内衣一件

4,给对方按摩5分钟

5,今天所有的衣服归你洗

6,对视,将对方手捧在脸旁,说五次:我爱你,我要嫁给你.

实行哪一条呢? 问题出来了。这个时候小明用有道词典翻译了上面的句子,写出了一个程序用来决定用哪个惩罚。

                    程序目的:产生随机数 1-6

                    执行方法:L 开始     G 结束运行,显示执行结果。

 1 #include  "stdio.h"
 2 #include "stdlib.h"
 3 #include "conio.h" 
 4 
5 6 #define TOTAL 6 7 8 #define TURE 1 9 #define FAULT 0 10 11 int main () 12 { 13 char *rules [6] = {"1 : initiative, tongue kiss more than 10s passionately.", 14 "2 : To lie, at the top, to do 10 pushups.", 15 "3 : To lie on both sides, in upper part, to do 10 pushups.", 16 "4 : Take off a piece of underwear.", 17 "5 : You have to wash all of the clothes.", 18 "6 : In the eye, take each other next to hand on the face, said five times: I love you, I want to marry you." 19 }; 20 21 char in_number ; 22 char in_key; 23 int i,j; 24 25 printf ("please enter 'L' to start the lover game : "); 26 scanf("%c" , &in_number); 27 while (in_number!='L') 28 { 29 printf (" input error !\n "); 30 printf ("please input 'L' to start the game "); 31 fflush(stdin); 32 scanf("%c" , &in_number); 33 } 34 35 printf ("please input 'G' to end "); 36 while (in_key != 'G') 37 { 38 i = (int)rand(); 39 i = i % (TOTAL); 40 scanf("%c" , &in_key); 41 if(in_key == 'G') 42 { 43 printf (" Accept the punishment! ! ! \n"); 44 printf ("THE NUMBER IS %d \n ",(i+1)); 45 printf ("%s\n\n\n", *(rules + i)); 46 } 47 } 48 system ("pause"); 49 }

 

        聪明的小红发现每次如果正确输入L G 输出的结果一直都是 6 。这个时候小红就抗议了,说小明的程序有问题,这六个数字不是随机的。是小明事先安排好的。小明有理说不清啊,只能继续研究。后来查阅到。C语言中 rand()随机函数是伪随机。不是真正的随机。
        既然是伪随机,那就要和自己的GF解释清楚啊,解释清楚了还要解决。于是在 37  38 行之间加上语句  :

          srand((unsigned)time(NULL));  

          头文件   #include <time.h>

          作用就是

          首先给srand()提供一个种子,它是一个unsigned int类型,其取值范围从0~65535

          然后调用rand(),它会根据提供给srand()的种子值返回一个随机数(在0到32767之间)

          根据需要多次调用rand(),从而不间断地得到新的随机数;

          无论什么时候,都可以给srand()提供一个新的种子,从而进一步“随机化”rand()的输出结果

 最终结果:

 1 //date : 13/11/25 
 2 //designer :pengxiaoen
 3 // function : lover game 
 4 
 5 #include  "stdio.h"
 6 #include "stdlib.h"
 7 #include "conio.h" 
 8 #include <time.h>
 9 
10 #define TOTAL   6UL
11 #define  TURE   1
12 #define  FAULT  0
13 
14 int main ()
15 {
16   char *rules [6] = {"1 : initiative,  tongue kiss more than 10s passionately.",
17                      "2 : To lie, at the top, to do 10 pushups.",
18                      "3 : To lie on both sides, in upper part, to do 10 pushups.",
19                      "4 : Take off a piece of underwear.",
20                      "5 : You have to wash all of the clothes.",
21                      "6 : In the eye, take each other next to hand on the face, said five times: I love you, I want to marry you."
22                      };                     
23   char in_number ;
24   char in_key;
25   int  i,j;
26 
27   printf ("please enter 'L' to start the lover game : ");
28   scanf("%c" , &in_number);
29   while (in_number!='L')
30   { 
31     printf (" input error  !\n ");
32     printf ("please input 'L' to start the game ");
33     fflush(stdin);
34     scanf("%c" , &in_number);
35   }
36   
37   printf ("please input 'G' to end : ");
38   while (in_key != 'G')
39   {
40     srand((unsigned)time(NULL));   
41     i = (int)rand();
42     i = i % (TOTAL);
43     scanf("%c" , &in_key);
44     if(in_key == 'G')
45      {
46         printf ("\n\n\n\n\n        Accept the punishment! ! ! \n");
47         printf (" THE NUMBER IS   %d\n ",(i+1));
48         printf (" %s\n\n\n\n\n", *(rules + i));
49      }
50   }
51   system ("pause");
52 } 

OK 搞定,给了时间种子,随机数每次执行都不一样了。基本上实现了随机性。

 总结:泡妞也要用头脑

 

 

 

 

 

 

 

 

posted on 2013-11-26 21:02  清霜一梦  阅读(1601)  评论(0编辑  收藏  举报