Kaucsenta

Generate Uniqeu Random Integer List

Oct 31st, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: kaucsenta
  4.  *
  5.  * Created on 2013. november 1., 0:19
  6.  */
  7.  
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <fstream>
  11. #include <sstream>
  12. #include <vector>
  13.  
  14. using namespace std;
  15.  
  16. int main() {
  17.     srand (time(NULL));
  18.     int N;
  19.     int newitem;
  20.     vector<int> array;
  21.     string file;
  22.     cout<<"Hany elemu random int listat akarsz?"<<endl;
  23.     cin>>N;
  24.     file="random"+static_cast<ostringstream*>( &(ostringstream() << N) )->str()+".txt";
  25.     ofstream myfile(file.c_str());
  26.  
  27.     for(int i=0;i<N;i++){
  28.     bool unique;
  29.     do
  30.     {
  31.       unique=true;
  32.       newitem=rand()%N;
  33.       for(int i1=0;i1<i;i1++)
  34.       {
  35.          if(array[i1]==newitem)
  36.          {
  37.            unique=false;    
  38.             break;
  39.          }
  40.       }
  41.     }while(!unique);
  42.     array.push_back(newitem);
  43.     cout<<array[i]<<endl;
  44.     }
  45.     N=array.size();
  46.     for( vector<int>::const_iterator i = array.begin(); i != array.end()-1; ++i)
  47.     myfile<< *i <<endl;
  48.     myfile<<array[N-1];
  49.  
  50. myfile.close();
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment