最透彻的关于“随机数种子”和“伪随机数”的产生原理

来源:互联网 发布:淘宝蝶恋花亚麻线 编辑:程序博客网 时间:2024/05/21 14:51

文章转自:http://blog.csdn.net/xzp7772009/article/details/7849030
版权归原作者!

这里写图片描述
这里写图片描述

//rand01.c#include<dos.h>static unsigned int RAND_SEED;unsigned int random(void){RAND_SEED=(RAND_SEED*123+59)%65536;return(RAND_SEED);}void random_start(void){int temp[2];movedata(0x0040,0x006c,FP_SEG(temp),FP_OFF(temp),4);RAND_SEED=temp[0];}main(){unsigned int i,n;random_start();for(i=0;i<10;i++)printf("%u\t",random());printf("\n");}

这里写图片描述
这里写图片描述
这里写图片描述

//rand02.cpp#include <iostream>#include <ctime>using namespace std;int main(){unsigned int seed=5;srand(seed);unsigned int r=rand();cout<<r<<endl;}

这里写图片描述

//rand03.cpp#include <iostream>#include <ctime>using namespace std;int main(){srand((unsigned)time(NULL));unsigned int r=rand();cout<<r<<endl;return 0;}

这里写图片描述

//rand04.cpp#include<iostream>#include<time.h>using namespace std;int main(){int rNum,m=20;char *ch=new char[m];for ( int i = 0; i<m; i++ ){//大家看到了,随机种子会随着for循环在程序中设置多次srand((unsigned)time(NULL));rNum=1+(int)((rand()/(double)RAND_MAX)*36); //求随机值switch (rNum){case 1: ch[i]='a';break ;case 2: ch[i]='b';break ;case 3: ch[i]='c';break ;case 4: ch[i]='d';break ;case 5: ch[i]='e';break ;case 6: ch[i]='f';break ;case 7: ch[i]='g';break ;case 8: ch[i]='h';break ;case 9: ch[i]='i';break ;case 10: ch[i]='j';break ;case 11: ch[i]='k';break ;case 12: ch[i]='l';break ;case 13: ch[i]='m';break ;case 14: ch[i]='n';break ;case 15: ch[i]='o';break ;case 16: ch[i]='p';break ;case 17: ch[i]='q';break ;case 18: ch[i]='r';break ;case 19: ch[i]='s';break ;case 20: ch[i]='t';break ;case 21: ch[i]='u';break ;case 22: ch[i]='v';break ;case 23: ch[i]='w';break ;case 24: ch[i]='x';break ;case 25: ch[i]='y';break ;case 26: ch[i]='z';break ;case 27:ch[i]='0';break;case 28:ch[i]='1';break;case 29:ch[i]='2';break;case 30:ch[i]='3';break;case 31:ch[i]='4';break;case 32:ch[i]='5';break;case 33:ch[i]='6';break;case 34:ch[i]='7';break;case 35:ch[i]='8';break;case 36:ch[i]='9';break;}//end of switchcout<<ch[i];}//end of for loopcout<<endl;return 0;}

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

原创粉丝点击