Hello I'm trying to test out a random number generator to see how the distribution is and I want to write the values I get from the number generator to a file. Can someone show me in a simple code how to do that, or a good website where I can see how to do it. Thanks!

Dani AI

Generated

Two separate problems explain why only the tenth value appears: the file write is executed after the loop finishes (so you only see the last i/j value), and the random-seed/usage is inconsistent (re-seeding inside the generator loop with time() can produce repeated or poor results). rightly pointed out the placement issue and posted a tutorial — below is a concise, robust pattern that avoids both pitfalls and uses modern C++ randomness.

Use these steps:

  • Seed the generator once at program start.
  • Open the output stream and verify it opened.
  • For each iteration generate values until the value meets your threshold (a do/while is ideal), then immediately write that iteration to the file.
  • Close the stream (or rely on RAII).

Example (modern C++11+):

#include <fstream>
#include <random>
#include <iomanip>

const double MINMASS = 0.0000515053396;

int main() {
    std::ofstream out("up.txt");
    if (!out) return 1;                  // check file open
    std::mt19937 rng(std::random_device{}());
    std::uniform_real_distribution<double> dist(0.0, 0.013365);

    for (int idx = 0; idx < 10; ++idx) {
        double value;
        do { value = dist(rng); } while (value < MINMASS);
        out << idx << '\t' << std::fixed << std::setprecision(9) << value << '\n';
    }
}

Notes and cautions:

  • Do not call srand/srand48 inside a loop — seed once. drand48 is POSIX-only; prefer <random> for portability and better quality.
  • Check the stream state before writing and set precision/formatting to control output.
  • For distribution analysis, output CSV or plain numbers and load them into Python/R/Excel to compute histograms or statistics.

Recommended Answers

All 6 Replies

I looked at the tutorial and I modified the code a bit but I can't get it to print out my random numbers. I know what I'm doing is probably sill, but I can't figure how to print out what I want. Here's the code:

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

int main(void)
{
      int i;
      srand( (unsigned)time( NULL ) );
      ofstream file;

     file.open("up.txt");		//open a file
     for( i = 0;   i < 10;i++ )
     file<<"%d file\n",rand();		//write to it
	
     file.close();				//close it



}
file<<"%d file\n",rand();

cant be dont this way, it is not printf() that you are using :)
try:

file << rand() << " file\n";

Sweet thanks

Can someone tell me why am I only getting the tenth entry to print out. Thanks.

#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define MINMASS  (0.0000515053396)
using namespace std;

int main()
{
      int i;
      double j1;
      j1 = drand48() * 0.013365;
      
      ofstream file;
    
 file.open("up.txt");		//open a file

     for( i = 0;   i < 10;i++ )
while(j1 <  MINMASS)
    {
      srand48(time(NULL));
      j1 = drand48() * 0.013365;
    }
       file<< i <<" "<<  j1 <<"\n" ;    	//write to it
	
     file.close();			//close it



}

becuse you are writing to the file after you are done with the loop. so put

file<< i <<" "<<  j1 <<"\n" ;

inside the loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.