c++生成随机数

#include <iostream>
using namespace std;

#include "time.h"
#include "windows.h"

template<typename type> void init_vector(vector<type>&v);
template <typename type> void print_vector(const vector<type> &v);

int VECTOR_SIZE = 10;
static unsigned int seed = 1;

int main(void)
{
 vector<int> v;
 seed = (unsigned int)time(NULL);//初始化种子
 
 init_vector(v);
 print_vector(v);

 init_vector(v);
 print_vector(v);init_vector(v);
 print_vector(v);init_vector(v);
 print_vector(v);
 init_vector(v);
 print_vector(v);

return 0;
}


template <typename type>
void print_vector(const vector<type> &v)
{
 vector<type>::const_iterator iter = v.begin();
 while (iter!=v.end())
 {
  cout << *iter++ << "\t";
 }
 cout << endl;
}

template<typename type>
void init_vector(vector<type>&v)
{
 srand(seed);//播种
 while (v.size()!=0)
 {
  v.pop_back();
 }
 while (v.size() != VECTOR_SIZE)
 {
  int val = rand()%100;
  v.push_back(val);
 }

 seed += 1; //改变种子
}


 

你可能感兴趣的:(C++,vector,null,iterator,include)