随机数

以前通过书和资料,看到了定义随机数的两种形式,就自认为自己理解透了,会用了。可是考试之后才发现自己的理解只是表面,并不透彻。
首先是先说一下刚开始对随机数的浅层理解(这里就用前边的实验来举例子)
第一种就是用书上定义种子seed的方法

#include <iostream>  
#include <cstdlib>   
using namespace std;  
int main()  
{  
    int x; 
	unsigned seed; 
    srand(seed);  
    int number=rand()%100+1;  
    cout<<"猜一下这个数: ";  
    cin>>x;  
    while(1)  
    {  
        if(x!=number)  
        {  
            if(x<number)  
            {  
                cout<<"小了"<<endl;  
            }  
            else  
            {  
                cout<<"大了"<<endl;  
            }  
        }  
        else  
        {  
            cout<<"恭喜猜对了!"<<endl;break;  
        }  
        cin>>x;  
    }  
    return 0;  
}

以seed为种子的时候,我发现每次电脑给出的随机数貌似都是一样的,如果想不一样的话呢,参照一些资料,我发现可以以时间为种子,这时候要定义一个头文件<time.h>
这时候程序如下:

#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
 int x; 
    srand(time(0));  
    int number=rand()%100+1;  
    cout<<"猜一下这个数: ";  
    cin>>x;  
    while(1)  
    {  
        if(x!=number)  
        {  
            if(x<number)  
            {  
                cout<<"小了"<<endl;  
            }  
            else  
            {  
                cout<<"大了"<<endl;  
            }  
        }  
        else  
        {  
            cout<<"恭喜猜对了!"<<endl;break;  
        }  
        cin>>x;  
    }  
    return 0;  
 } 

这是我在这次考试之前对随机数的理解
考试的第一题呢,我当时看到是随机数就很开心,然后比较自以为是的用time.h的头文件写了以下程序

#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
class Dice{
	public:
		Dice(int n):sides(n){	};
		int cast()
		{
		 srand(time(0));  
         int number=rand()%6+1;  
		}
		private:
			int sides;
};
int main()
{
	int x;
	float count;
	cin>>x;
	Dice a(40);
	for(int i=0;i<=500;i++)
	{
	int s=	a.cast();
		if(s==x)
		count++;
		
	}
	float c=count/500;
	cout<<c<<endl;
	return 0;

}


程序一直是错的,就很崩溃,考试的时候呢,也没时间多想。考试之后经过查资料和思考,才知道运用time.h头文件的时候,那个参数time(0)是相当于当前时间,所以for函数运用这个随机数的时候,时间都是一样的,相当于参数都是一样的,所以才会错
所以我把程序改成了以下的样子

#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
class Dice{
	public:
		Dice(int n);
		int cast()
		{  
         int number=rand()%sides+1;  
         return number;
		}
		private:
			int sides;
};
Dice::Dice(int n)
{
	sides=n;
}
int main()
{
	int x;
	float count=0;
	cin>>x;
	Dice a(40);
	for(int times=0;times<=500;times++)
	{
	
	srand(times); 
	int s=a.cast();
		if(s==x)
		count++;
		
	}
	float c=count/500;
	cout<<c<<endl;
	return 0;
}


结果呢还是不太对,每次运行的时候概率都是一样的,这就相当于一个伪随机数吧,因为系统运行是很快的,所以根据同学和老师的建议我又改动了这个程序

#include<iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
class Dice{
    public:
        Dice(int n);
        int cast()
        {  
         int number=rand()%sides+1;  
         return number;
        }
        private:
            int sides;
};
Dice::Dice(int n)
{
    sides=n;
}
int main()
{
    int x;
    float count=0;
    cin>>x;
    Dice a(40);
    srand((unsigned)time(nullptr));//放在外边就可以了,因为放在循环里面的话,系统运行的太快了,时间都差不多 
    for(int times=0;times<=500;times++)
    {
    
   // srand((unsigned)time(nullptr));
   //srand(times);这是我原来写的,这个其实也相当于一个伪随机数 
    int s=a.cast();
        if(s==x)
        count++;
        
    }
    float c=count/500;
    cout<<c<<endl;
    return 0;
}

或者改成这个样子

#include<iostream>
#include<cstdlib>
#include<ctime>
//#include<time.h>
using namespace std;
class Dice{
    public:
        Dice(int n);
        int cast()
        {  
         int number=rand()%sides+1;  
         return number;
        }
        private:
            int sides;
};
Dice::Dice(int n)
{
    sides=n;
}
int main()
{
    int x;
    float count=0;
    cin>>x;
    Dice a(40);
    
    for(int times=0;times<=500;times++)
    {
    srand(times);

    int s=a.cast();
        if(s==x)
        count++;
        
    }
    float c=count/500;
    cout<<c<<endl;
    return 0;
}

这样改掉头文件也对了
终于把程序改对了,同时呢,对于随机数也加深了理解,以后再运用的时候也会更加自如了

posted @ 2018-05-18 22:43  雪卿狂  阅读(284)  评论(0编辑  收藏  举报