C: Generate random 20 digit number

Associate
Joined
18 Mar 2007
Posts
291
Hi guys.

Would the best way to go about this be to generate a random digit between 0 and 9, convert it to a string, then generate another random digit between 0 and 9. Concatenate them and repeat this process?

Is there another way?

Cheers, Rob
 
I have no idea whether this will work as I am not at work and don't have an IDE at home. I just knocked this up in notepad.

Code:
#include <stdlib.h>		/* For rand() and srand() */
#include <time.h>		/* For time() */
#include <stdio.h>		/* For sprintf() and prinf() */


/* Definition of how many random numbers you want */
#define NUMBER_OF_RANDOMS	10


int main(int argc, char *argv[] )
{
	short int nCount = 0;
	short int nOffset = 0;
	int nRandomNum = 0;

/*
	Array of chars one bigger than NUMBER_OF_RANDOMS so the 
	last character will be a NULL '\0' . This relies on you
	only generating random numbes which are one digit long
*/
	char strStringOfRands[ NUMBER_OF_RANDOMS+1 ] = { ' ', };

/* 	
	Seed the rand with the current time so each time the 
	program is run the numbers should be different 
*/
	srand( (unsigned)time( NULL ) );

	for( nCount = 0; nCount < NUMBER_OF_RANDOMS; nCount++ )
	{
		/* Generate a random number between 0 and 9 */
		nRandomNum = rand() % 10;

		/* Put the integer into the string */
		nOffset = sprintf( strStringOfRands + nOffset, "%d", nRandomNum );
	}

	printf( "Your %d random numbers are: %s",NUMBER_OF_RANDOMS, strStringOfRands );

	return (0);
}
 
Casanova's code seems to only generate two digits. Change the nOffset line to this and it seems to work:

Code:
sprintf( strStringOfRands + nOffset, "%d", nRandomNum );
nOffset = nOffset +1;

Also change NUMBER_OF_RANDOMS to 20 rather than 10.

Full code:
Code:
#include "stdafx.h"
#include <stdlib.h>		/* For rand() and srand() */
#include <time.h>		/* For time() */
#include <stdio.h>		/* For sprintf() and prinf() */


/* Definition of how many random numbers you want */
#define NUMBER_OF_RANDOMS	20


int main(int argc, char *argv[] )
{
	short int nCount = 0;
	short int nOffset = 0;
	int nRandomNum = 0;

/*
	Array of chars one bigger than NUMBER_OF_RANDOMS so the 
	last character will be a NULL '\0' . This relies on you
	only generating random numbes which are one digit long
*/
	char strStringOfRands[ NUMBER_OF_RANDOMS+1 ] = { ' ', };

/* 	
	Seed the rand with the current time so each time the 
	program is run the numbers should be different 
*/
	srand( (unsigned)time( NULL ) );

	for( nCount = 0; nCount < NUMBER_OF_RANDOMS; nCount++ )
	{
		/* Generate a random number between 0 and 9 */
		nRandomNum = rand() % 10;

		/* Put the integer into the string */
		sprintf( strStringOfRands + nOffset, "%d", nRandomNum );
		nOffset = nOffset+1;
	}

	printf( "Your %d random numbers are: %s",NUMBER_OF_RANDOMS, strStringOfRands );

	return (0);
}
 
Cheers for testing Pho,

I originally had the loop

Code:
for( nCount = 0; nCount < NUMBER_OF_RANDOMS; nCount++ )
{
	/* Generate a random number between 0 and 9 */
	nRandomNum = rand() % 10;

	/* Put the integer into the string */
	sprintf( strStringOfRands + nCount , "%d", nRandomNum );
}

So using the nCount variable to offset into the char Array. But then I thought, what happens if he changes the random number to generate numbers greater than 9.

I thought the sprintf function returns the number of bytes written to the string. But I used the call

nOffset = sprintf( strStringOfRands + nOffset, "%d", nRandomNum );

Rather than

nOffset += sprintf( strStringOfRands + nOffset, "%d", nRandomNum );

I knew I would have made some errors, I find it really difficult to code without an IDE and MSDN, lol


Thanks again Pho
 
Back
Top Bottom