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.


|