c++ 读文件产生随机数

读文件得第一行的数字作为随机数的最大值。

随机数写入文件中,这个代码随机产生了1000个点对。

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)

int main(int args, char **argv)
{
    std::ifstream fin("/home/a.txt", std::ios::in);
    std::ofstream fout("/home/b.txt", std::ios::out);

    std::string line;
    int up_bounder;
    getline(fin, line);
    int i=0;
    std::stringstream word(line);

    word>>up_bounder;
    srand((int)time(0));
    while(i<1000){
        fout<<random(up_bounder)<<" "<<random(up_bounder)<<"\n";
        ++i;
    }

    fin.clear();
    fin.close();
    fout.clear();
    fout.close();

    return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章