Ok, the following is my code.. I don't understand two things.. 1) I keep getting the same random numbers..? What do I need to change for them to keep being random and not the same everytime I run the program? 2) When I run the program I get the following:
File data.txt was opened successfully.
A new random number ..... 41
Want to write another number to dataFile? (y/n) =>y
A new random number ..... 6334
Want to write another number to dataFile? (y/n) =>y
A new random number ..... 19169
Want to write another number to dataFile? (y/n) =>y
A new random number ..... 11478
Want to write another number to dataFile? (y/n) =>y
A new random number ..... 26962
Want to write another number to dataFile? (y/n) =>y
A new random number ..... 5705
Want to write another number to dataFile? (y/n) =>n
File dataFile is done.
5705
18467
26500
15724
29358
24464
28145
There are 7 numbers in data.txt.
The maximal is 29358
The minimal is 15724
The numbers that are said to be writing into the file aren't the ones that are coming up... and I think it is skipping the first number since it states the minimum is actually another number and not 5705? Well, actually, 41 should be included also, so 41 should be the minimum? Please help.. This is confusing me even more..
#include < iostream >
#include <fstream >
#include < string >
#include < cstring >
#include < cmath>
#include <cstdlib>
#include <iomanip>
usingnamespace std;
int main( )
{
//declare infile and output files
ifstream inFile; //input file object
ofstream dataFile; //output file object
int number, //var to store an int value
min, max; //vars to store the minimal and maximal int values
char flag; //a flag to stop the loop
/***************************************************
Part A. create a data file and write a list
a random integers into it.
***************************************************/
//open dataFile
dataFile.open("data.txt");
//test if there is a failure or not
if(!dataFile)
{
cout<<"Failed to open input file data.txt."<<endl;
return 1; //terminate the program
}
else
cout<<"File data.txt was opened successfully."<<endl;
/***************************************************
Part B. Write a list a random numbers to dataFile
***************************************************/
//use a loop to write random numbers to dataFile
//one at a time
while (true)
{
number = rand(); //generate a random number
dataFile<<endl<<rand(); //write a random number to data.txt
cout<< "A new random number ..... " << number <<endl;
//show the random number
//write more numbers?
cout<<"Want to write another number to dataFile? (y/n) =>";
cin>>flag;
//stop if do not want
if (flag !='y' && flag != 'Y')
break;
}
cout<<"File dataFile is done. " <<endl;
//close dataFile
dataFile.close();
/******************************************************
Part C: open file data.txt for input
******************************************************/
inFile.open("data.txt");
//use a loop to keep reading integers from inFile, one at a time,
//until the all integers in the have been read.
int i=0; //var to store iteration number
while (inFile)
{
cout<<number<<endl;
inFile >> number;
if (i==0) //first loop iteration
min = max = number;
if (i !=0) //other iteration
{
if (number > max)
max = number;
if (number < min)
min = number;
}
i = i+1; //increase the iteration number
}
//close inFile
inFile.close();
//show reault
cout << " There are " << i << " numbers in data.txt."<<endl;
cout << " The maximal is " << max <<endl
<< " The minimal is " << min << endl;
cout<< "Byebye! " <<endl;
//well done and exit
return 0;
}
1.) put srand(time(NULL)); at the beginning of your program to seed rand() so it is actually random
2.) Your are not actually writing the random number to the text, you are writing another generator of rand()
3.) Fix the above and #3 should be fixed
Any random generator doesn't really generate a random sequence of numbers.
For a given algorithm on a platform the sequence is determined by the input commonly known as 'seed'. So You need to feed each time different seed to random generator.
You are right and wrong. I could get into a philisophical debate with you as to what is the definition of "random" (as your first statement is an oxymoron at best), however I'll to the coding side of things.
It is never recommended to seed the rng more than once in a program. The C RNG, for example, is an LCRNG which will generate every integer in the sequence 0...2^32-1 exactly once, in some order, before repeating the sequence. That means if you are using all 32-bits of the generated numbers, yes, the sequence is quite non-random. However, if you are using fewer bits, say 10 (generating numbers in the range [0...1023]) then each integer will be generated multiple times and the sequence will appear, to the human eye at least, to be random.