#include "stdafx.h" #include "iostream" #include "fstream" //写入到文件中要用的头文件 #include "ctime" //time(0) 要用的头文件爱你。 #include "cstdlib" //使用随机数生成要添加的头文件 #include "vect" using namespace std; using VECTOR::Vector; int main() { ofstream fout; //定义文件的指向标 fout.open("thewalk.txt"); //创建一个文件 using VECTOR::Vector; srand(time(0)); //time(0); 其中用0做为地址参数。 double direction = rand() % 360; //产生随机数 fout << result << endl; //打印出所要的方向 ,调用operator<< (fout, result), 来进行写入到文件中 }
rand()函数要用一个初始种子值来获得随机函数,该随机值将用作下一次函数调用的种子。
srand()允许覆盖默认的种子值,重新启动另一个随机数序列,在这里time(0)(当前时间,通常为某一日期开始的秒数)返回值设置种子。