file:///U|/docs/OA/articles/Mathématiques/maz_nfr_sm1.txt[13.05.2011 15:52:29]
// Random sampling algorithm //
// Authors: Petar Pjanic, Milos Pjanic //
// Date: 28/10/2008
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
int main(int argc, char ** argv) {
vector <string> vec;
vector <long int> vec2;
ofstream myfileout;
ifstream myfilein;
string st;
long int i,value,n,z,value1,value2;
z=1;
if(argc<3){cout <<"error";return 0;}
// "myfileout" - output file // "myfilein" - input file myfileout.open (argv[1]);
myfilein.open (argv[2]);
// "n" - desired number of randomly chosen lines
cout <<"Enter desired number of randomly chosen lines:"<< endl;
cin >> n;
// Seeding of the random function with the current time srand((unsigned)time(0));
file:///U|/docs/OA/articles/Mathématiques/maz_nfr_sm1.txt[13.05.2011 15:52:29]
// Reading of the input file // "z" - number of input lines
while(getline(myfilein,st).eof()==false) {
vec.push_back(st);
z++;
}
vec.push_back(st);
// Generation of n random numbers for( i=0; i < n; )
{
value1=rand();
value2=rand();
if(value1<32000 && value2<32000) {
value=((int)(((double)value1/((double)32000))*1000))*1000 + ((int)(((double)value2/((double)32000))*1000));
if(value< z) {
if(search(vec2.begin(),vec2.end(),&value,&value+1)==vec2.end()){
vec2.push_back(value); i++; cout<< value<<endl;}
} } }
// Making the output file out of n randomly chosen lines for the input file
for( i=0; i < n;i++) {
value=vec2[i];
myfileout << vec[value]<<endl;
}
// End
return 0;
}