samedi 21 avril 2018

Generating random numbers in a file, and calling them from the file, then printing them out

I'm trying to create a file that generates random numbers, then I want the program to call those random numbers and print them out. 1st; the srand (time(NULL)) function is giving me an error, 2nd; i want to check wether the loop and the formula i used to get random numbers are correct.

thanks.

#include <iostream>
#include <fstream>
#include<stdlib.h>
#include <time.h>

using namespace std;
int rand(int x);

int main() {


ofstream outputFile;
outputFile.open("fileTest.txt");
// unsigned seed = time(0);

// srand(time(NULL));
int i;
for (i =0; i<23; i++){
outputFile << rand(i) <<endl;
}

ifstream inputFile;
inputFile.open ("fileTest.txt");

      int ranNum[i];

    for (int i = 190; i <= 270; i++)
    {
        ranNum[i] = (rand() % (270-190+1)+ 190);
        inputFile>> ranNum[i];
        cout<<ranNum [i]<<" "<<endl;
    }

  system("pause");

  return 0;
}




Aucun commentaire:

Enregistrer un commentaire