I need to create a random number generator which generates white noise (equally distributed random numbers) into a text file as output. I thought to use for cicle with rand() function, but i don't understand perfecly (after reading the function definition) how it works, and how can i write the generated numbers into a text file. Any help would be appreciated, thanks beforehand.
int main() {
srand(time(NULL));
for(int i = 0; i < 1000; ++i) {
value = rand()%1;
}
return 0;
}
Which is perfect for my aims, as far as i've understood it generates random numbers up to 1, but i don't know how (reading the tutorials so far) to write those into a the text file (the randoms, and i values inside the cicle also).
If using C++ you need to #include<fstream> declare an ofstream eg: ofstream file ( "myfile.txt" ); and use the << operator: file << i << ' ' << value << '\n';
Anyway, in the code you posted 'value' isn't declared
Thank you, I made some progress, I've got the .txt file after using the compiler but it seems empty (sized 0 kbytes, and empty when opened), and i can't figure it out why. Any suggestions where i went the wrong way?
You don't want rand() % 1 as that will always be 0. rand() % 100 will give numbers between 0 and 99, inclusive. (Be forewarned, however, that the first few lowest numbers are somewhat overrepresented.)
Thank you I've made the code more readable, but still haven't soluted that I got an empty file after compiling it, instead of a file with random numbers...
I've figured it out with help, thank you all for helping!