473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generate fixed-length string containing random digit.

Hi,
I am new to C. How to generate an fixed-length string containing an random
digits? for example string of 5 characters, the value can be 03234 or 23423
or 02343

Thanks
Tran Hong Quang

Feb 25 '06 #1
3 2515
> Hi,
I am new to C. How to generate an fixed-length string containing an random
digits? for example string of 5 characters, the value can be 03234 or
23423
or 02343

Hi,

something like this should do the trick

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

int main( void )
{
int i;
char randString[6] = "";

srand( (unsigned)time( NULL ) );
i= rand() ;
sprintf(randString, "%5d", i);
}

this example gets a random number (an integer) and prints the first 5
characters in your fixed size string.
temember that you need an extra character to allow for null
termination.hence randString[6] instead of randString[5].

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Feb 25 '06 #2
Bruno van Dooren wrote:
Hi,
I am new to C. How to generate an fixed-length string containing an random
digits? for example string of 5 characters, the value can be 03234 or
23423
or 02343


Hi,

something like this should do the trick

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

int main( void )
{
int i;
char randString[6] = "";

srand( (unsigned)time( NULL ) );
i= rand() ;
sprintf(randString, "%5d", i);
}

this example gets a random number (an integer) and prints the first 5
characters in your fixed size string.
temember that you need an extra character to allow for null
termination.hence randString[6] instead of randString[5].


Bruno:

Shouldn't that be

sprintf(randString, "%05d", i);

David Wilkinson
Feb 25 '06 #3
this example gets a random number (an integer) and prints the first 5
characters in your fixed size string.
temember that you need an extra character to allow for null
termination.hence randString[6] instead of randString[5].


Bruno:

Shouldn't that be

sprintf(randString, "%05d", i);

David Wilkinson


you are partially right. there is still a second problem with my example :-)
doing it like this assures only that there are minimum 5 characters, all of
which will have
a numerical value between 0 and 9.

if i has more than 5 decimal digits it would try to print out all characters
with a buffer overflow as the result.

to do it correct you have to make sure that i is never more than 5 digits.
the correct solution in that case is this:
sprintf(randString, "%05d", i%100000);

That'll teach me to post an example without testing it first ... :-)

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Feb 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anna | last post by:
Hi all. I have probably a rather stupid question. If there is an HTML document, XML-formed using JTidy, is there any tool to convert it to valid XHTML? I.e. so that all the tags and attribute...
0
by: Jason | last post by:
.... or would you like me to give you some open source code on how to generate a unique id quickly. alternatively use your guid or fire the developer - take your pick.
9
by: pout | last post by:
What are the purposes of fixed-point? When should it be used? I read: #define Int2Fixed(x) (((long)(short)x) << 16) and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to...
1
by: Dan A | last post by:
Why can't .NET generate stubs to correspond to the exceptions that are thrown by a webservice? The exceptions that are thrown by an API are just as importantant as the return objects, etc. ...
4
by: Otie | last post by:
Hello, I am using the MSFlexGrd Control in VB5. I have 1 fixed row and one fixed column. I am trying to do a sort when the user clicks a column in the FIXED ROW. But when I capture the row...
1
by: Bruce | last post by:
I am using the Express version of VS2005 If I set Generate XML Documentation Files to yes then the xml file is generated but my OBJ files do not get generated. Any idea why? I am using the...
82
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure...
5
by: grasshopper2 | last post by:
I am looking to build a regular expression that will match pTOC(somenumber) where the number varies between 1& ten, this is to generate a table of contents file that I desperately need to display...
4
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
12
by: zandiT | last post by:
hello again i have almost finished with my database. i have decided to generate the reports by using a date or date range and i can't get it to work. first i used parameters in a query but its...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.