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!

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";

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.