random number generation into text file

Hi all!

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.
Last edited on
What do you have so far? Folks here are not going to do your homework for you.
Correct, as always, so far i have:

//white noise generator

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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).
Last edited on
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


Tutorial on C++ files: http://www.cplusplus.com/doc/tutorial/files/
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?

The code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//white noise generator

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace std;

int main() {
    srand(time(NULL));
    float value=0.0;
    ofstream myfile ("feherzaj.txt");
    myfile.open ("feherzaj.txt", ios::out | ios::app );
    if (myfile.is_open())
    {
                         for (int i = 0; i < 1000; ++i) {
                             value = rand(); 
                             myfile << i << " " << value << "\n";
                             }
    myfile.close();
    }
    else cout << "Unable to open file";
    return 0;
}

The file also appears to be opened, because the code haven't triggered else option.

A sidewalk, but my curiosity is limitless, how you highlight your code on this page, to make it more readable?
Last edited on
A sidewalk, but my curiosity is limitless, how you highlight your code on this page, to make it more readable?


http://www.cplusplus.com/forum/articles/1624/
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!
Last edited on
Topic archived. No new replies allowed.