Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random number output to text file

Status
Not open for further replies.

BrianCPP

Technical User
Sep 20, 2002
3
0
0
US
How do I output 50 numbers to a text file.
 
This is sample code to print out a 5 by 6 array of lotto numbers...I'm not sure if this is what you need...


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


void GenerateNumbers( int lotto[][6] );



main()
{

int lotto[5][6], r, c;

srand(( unsigned ) time(NULL ));

GenerateNumbers( lotto );

for ( r=0; r < 5; r++ )
{
for ( c=0; c<6; c++ )
{
cout << lotto[r][c] << &quot; &quot;;

}
cout << &quot;\n\n&quot;;

}

cout << &quot;\n\n&quot;;


}


void GenerateNumbers(int lotto[][6] )
{
int num, r, c;
for ( r=0; r < 5; r++ )
{
for ( c=0; c<6; c++ )
{
num = rand() % 47;
lotto[r][c] = num;

}
}




}



 
Better use it

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

int main(int argc, char* argv[])
{
int i;
FILE *fp;
fp=fopen(&quot;c:\\newtext.txt&quot;,&quot;w+&quot;);

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
{
char szTemp[64];
printf( &quot; %6d\n&quot;, rand() );
_itoa(rand(),szTemp,10);
strcat(szTemp,&quot; &quot;);
fputs(szTemp,fp);
}
fclose(fp);
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top