Member Avatar for Jackk123

Hello there my second topic
I need to write a function that writes 10 random numbers into a file.

I came up to this point:

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int main(){
	ofstream outfile("C:\\Random"); //Random as in you can put any file you want
	for(int i = 0; i != 10; ++i){
	outfile<< rand() << endl;
	outfile.close();
	return(0);
}
}

This is half wrong i think
I need help
thanx for reading

Recommended Answers

All 12 Replies

Your code should work, but I see some ways to improve it.

You haven't given the stream an output mode, but it should be okay, it'll default to ios::out|ios::trunc.

Your for loop will work, but in this situation you're better off using a less-than instead of a not-equal.
Q. What happens if i starts at 11 instead of 0?
A. You have an infinite loop. Using i < 10 instead keeps the loop from even starting.

const int MAX_NUMS = 10;
for(int i = 0; i < MAX_NUMS; ++i) {
  /* ... do something MAX_NUMS times ... */
}

You didn't "seed" the Random Number Generator (RNG), it's good practice to do so, but it's probably really not necessary for this. Unless, you need 10 different numbers every time the program is run. As written, you will get the same 10 numbers every time the program runs (it's because of how the RNG works, best not to elaborate ATM). Read up on the srand() function for more information.

Member Avatar for Jackk123

Thank u for posting the answer
I need random numbers every time
its run
and could u test the code if u can
tnx

also my code
was generating only 1 number
and it was always the same

I didn't say anyting the first time, but please use [code] ...code tags... [/code]. They make your code easier to read.

I missed a couple issues because the code you posted isn't formatted. Your return statement and close statement are part of your for loop. As a result, the program closes the file and terminates after generating only 1 number. You'll have to move those statements out of your loop.

To generate a different set of numbers every time, you will need to seed the RNG. I mentioned how to do this in my previous post.

also my code
was generating only 1 number
and it was always the same

Didn't see this part, your edit didn't show up until now.

It's only generating one number because of the positions of your close and return. To make the issue easier to see, I have re-formatted your code based on your blocking:

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int main(){
  ofstream outfile("C:\\Random"); //Random as in you can put any file you want
  for(int i = 0; i != 10; ++i){
    outfile<< rand() << endl;
    outfile.close();
    return(0);
  }
}

Notice where outfile.close(); and return(0); are...

You may want to review some formatting guidelines. (Thank WaltP for the article if you see him.)

The reason it always generates the same number is because of how the RNG works. This is rectified by seeding the RNG.

@OP: outfile closes when its destructor is called, and main returns a int implicitly.
So all that means is you don't need(according to your program) the outfile.close()
and return(0)] statement. Also as mentioned already, you need to seed the rand(),
to see rand, just add srand(time(0)) before ofstream outfile.... And you
also need to add #include<ctime> header.

Member Avatar for Jackk123

Thank u for u help
but i did use

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
       
int main(){
     ofstream outfile("C:\\Random.txt"); //Random as in you can put any file you want
     for(int i = 0; i != 10; ++i){
       outfile<< rand() << endl;
       outfile.close();
       return(0);
	}
}

Soo the first time i ran it
it generated 1 number which was 41 to the Random.txt file
and i made another txt file and change the name
but it didnt work and it didnt close
???
tnx again

The code that I posted was a reformatted version of the code you posted in your OP, nothing more, nothing less. I made no functional changes.

For the code to work the way you intend, the close brace on Line 12 needs to be moved up to between Lines 9 and 10.

Fix that and learn how to seed the RNG and you'll be all set.

Member Avatar for Jackk123

Heres what i got

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
       
int main(){
	ofstream outfile("C:\\Random.txt"); //Random as in you can put any file you want
	for(int i = 0; i != 10; ++i){
      outfile<< rand() << endl;
	  }
      outfile.close();
	  return(0);
}

And everything worked fine
but it didnt close
um i tested it 1 time
ill test more in the morning
And also
im new at the community here
soo i dont know wut guides u have
cuz the rand guide that i sow made me come here
but it didnt offer the right thing that i needed but is ok
tnx anyways

Member Avatar for Jackk123

Hey i tested it more
now it doesnt close
Anyways every time i copy paste it here it gets messed up

As suggested by firsPerson, you need to add a seed for rand (srand) and closing the file is useless (it will be automatic when the program finishes and outfile is deleted). So, try this (and indent better in the future!):

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
       
int main(){
  ofstream outfile("C:\\Random.txt"); //Random as in you can put any file you want
  srand(time(0));
  for(int i = 0; i != 10; ++i){
    outfile<< rand() << endl;
  };
  return(0);
};

Heres what i got

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
       
int main(){
	ofstream outfile("C:\\Random.txt"); //Random as in you can put any file you want
	for(int i = 0; i != 10; ++i){
      outfile<< rand() << endl;
	  }
      outfile.close();
	  return(0);
}

And everything worked fine
but it didnt close
um i tested it 1 time
ill test more in the morning
And also
im new at the community here
soo i dont know wut guides u have
cuz the rand guide that i sow made me come here
but it didnt offer the right thing that i needed but is ok
tnx anyways

To generate a "random" number, you call rand(). You seem to understand that. What you don't seem to understand is how random number generation works. Most computerized RNGs generate a predictable pattern of numbers. It always starts with the same number and produces the same series over and over. They call these "pseudo-random" numbers. To improve the level of randomness, you need to "seed" the RNG with a starting value to change the position within the pattern where it starts returning numbers.

I'm going to give you a link (at least 3rd time now, not counting mike's and firstPerson's mentions), I want you to follow it and read it. For the RNG to work correctly, and produce numbers that have an improved level of "randomness" you need to seed the RNG. To seed it you need to call the function srand() once near the beginning of your program before you ever call rand().

Seed the RNG with srand().(clicky)

You can generate random numbers within certain ranges, but I'll hold off on the technique for that for now to prevent further confusion.

Member Avatar for Jackk123

Fbody I know how to generate random numbers within certain ranges
i read that guide yesterday
Anyways thanx for the help

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.