Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: Sorry, newb...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 12 Topic 1058 of 1133
Post > Topic >>

Re: Sorry, newbie question about generating a random string

by David Thompson <dave.thompson2@[EMAIL PROTECTED] > Mar 28, 2008 at 07:20 PM

On Sat, 8 Mar 2008 12:21:06 -0600 (CST), Scooter <slbentley@[EMAIL PROTECTED]
>
wrote:

> I'm trying to write a simple program to generate a random string. I
> have what seems to work, although I'm guessing there are better ways
> to do it. But my overall goal is to generate a random character, then
> add a second random character to it, then another, etc. until the
> string grows to a max of 10 characters. I'm testing a word wheel on a
> website but have to use 'C' and it is not my forte'. Anyway, here's

Actually you are (almost) generating random uppercase letters, as
would make sense for an English word. Characters can also be, and a
string can contain, digits, punctuation, space and control characters.
I assume you understand that a sequence of uniformly-random letters
has little chance of being a valid word, especially above 3 letters
and not great even below -- but that's an algorithm problem not C.

> what I have:
>=20
> int myRand,myLoop;
> char myChar[1];
> char myLoc[11];

I'm presuming these declarations (variables) are within a function,
either main() or some other function you execute, see below.

>=20
> srand((unsigned int)time(0)); //Seed number for rand()
>=20
This isn't an absolutely ****table way of seeding, but it's good enough
for most purposes. You don't need the cast if you have an
#include <stdlib.h> at the top of your source as you should.

> for (myLoop =3D 0; myLoop < 10; myLoop++) {
>         myRand =3D rand() % 25 + 65;

There are other ways of 'selecting' the randomness (entropy) which are
sometimes somewhat better, but this is good enough. And this assumes
ASCII which again isn't totally ****table but good enough, but even in
ASCII it only does A .. Y not Z.=20

>         myChar[0] =3D (char)myRand;
> 	myLoc[myLoop] =3D  myChar[0] ;

The cast and copying is completely unnecessary. In C, unlike some
other programming languages, a character (code) IS an integer, just
one with a smaller range (usually 0-255) and size than 'int'. You
could simply do:
      myLoc[myLoop] =3D rand() % 26 + 65; /* not 25 */
or slightly clearer use the character literal:
      myLoc[myLoop] =3D rand() % 26 + 'A'; /* instead of 65 */

> 	printf("%s", myLoc);

This shouldn't produce the output you show below, since it outputs
each string (possible word) without a newline. You want:
      printf ("%s\n", myLoc);
or more simply:
      puts (myLoc);
and you should have #include <stdio.h> at the top of your source.

> }
>=20
>=20
>=20
> I would expect the output to look something like:
> V
> VD
> VDY
> VDYW
> VDYWQ
> VDYWQA
> VDYWQAE
> VDYWQAEM
> VDYWQAEMQ
> VDYWQAEMQJ
>=20
> But instead it looks more like:
> V
> VD
> VDY
> VDYW=A6=0EB
> VDYWQ=0EB
> VDYWQAB
> VDYWQAE
> VDYWQAEM=FC=0EB
> VDYWQAEMQ=0EB
> VDYWQAEMQJB

If you actually have a newline (or puts) as above, this output would
occur because your variable myLoc is local (or more precisely
automatic) and uninitialized. Global or static variables that you
don't initialize are implicitly initialized to zero, which for a
string is The Right Thing. (For other types it often is as well, but
that's not at issue here.) But an automatic variable that you don't
initialize usually contains garbage. Apparently your myLoc contained
zero in bytes 1 to 3, so storing into 0 to 2 happened to 'luck out'
because there was a null byte terminating the string after the new
character. However bytes 4 to 6 seem to be garbage nonzero values, so
storing into 3 'exposed' them, and only after you stored into 4 to 6
were they all changed to valid values. Similarly for 8 to 10, except
that you stop before storing to 10.

- formerly david.thompson1 || achar(64) || worldnet.att.net
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 12 Posts in Topic:
Sorry, newbie question about generating a random string
Scooter <slbentley@[EM  2008-03-08 12:21:06 
Re: Sorry, newbie question about generating a random string
Keith Thompson <kst-u@  2008-03-17 15:19:13 
Re: Sorry, newbie question about generating a random string
=?utf-8?Q?Dag-Erling_Sm=C  2008-03-28 19:20:30 
Re: Sorry, newbie question about generating a random string
Keith Thompson <kst-u@  2008-03-30 14:58:21 
Re: Sorry, newbie question about generating a random string
Jack Klein <jackklein@  2008-03-17 15:19:52 
Re: Sorry, newbie question about generating a random string
Jonathan Leffler <jlef  2008-03-17 15:21:03 
Re: Sorry, newbie question about generating a random string
Barry Schwarz <schwarz  2008-03-17 15:21:22 
Re: Sorry, newbie question about generating a random string
Carl Barron <cbarron41  2008-03-17 15:21:31 
Re: Sorry, newbie question about generating a random string
Francis Glassborow <fr  2008-03-17 15:21:40 
Re: Sorry, newbie question about generating a random string
Kenneth Brody <kenbrod  2008-03-17 15:22:01 
Re: Sorry, newbie question about generating a random string
Vinicius Pinto <vinici  2008-03-17 15:22:07 
Re: Sorry, newbie question about generating a random string
David Thompson <dave.t  2008-03-28 19:20:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:23:22 CDT 2008.