c++随机数生成

#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
//#define random(a,b) (rand()%(b-a+1)+a)
int main()
{
   srand((unsigned)time(NULL));
   vector<int> numbers(10);
  // std::srand(std::time(0));
   int a =std::rand();
   cout << a <<endl;
   //rand 函数名做参数
   generate(numbers.begin(),numbers.end(),rand);
   for(int i=0;i<numbers.size();i++)
   {
       cout<<numbers[i]<<' ';
      
   }
    cout<<endl;
    //产生0-9之间的随机数
   vector<int>::iterator iter;
   for(iter=numbers.begin();iter!=numbers.end();iter++)
   {
       cout<<*iter%10<<' ';
   }
    
    
    return 0;
}

/*#include<iostream>
#include<cstdlib>
#include<ctime>
#define random(a,b) (rand()%(b-a+1)+a)
using namespace std;
int main()
{
    srand((unsigned)time(NULL));
    for(int i=0;i<10;i++)
    cout<<random(1,100)<<' ';
    return 0;
}*/
1893582522
465463252 1204446276 894052582 1112819315 665679593 1909039033 545805198 863268941 725740385 2063878193 
2 6 2 5 3 3 8 1 5 3 


猜你喜欢

转载自blog.csdn.net/runner668/article/details/80158533