Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > random words

Reply
Thread Tools

random words

 
 
Olaf \El Blanco\
Guest
Posts: n/a
 
      05-02-2006
How can i generate random words? ('a'..'z')

Is there any function that convert a number to it ascci char?

My english is horrible! Here an example:
function(65)
return 'a';

Thank you!



 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      05-02-2006
Olaf "El Blanco" said:

> How can i generate random words? ('a'..'z')


If you just want random collections of letters, use srand to seed your
pseudorandom number generator (with the result of a call to time(NULL) if
you don't want successive runs to repeat the same results), once only.

Put your "alphabet" into an array (this allows you easy flexibility - for
example, you might want some letters to be more common than others), and
keep track of its length, e.g.:

char alphabet[] = "aaaaaaaaaaabbbccccddddddeeeeeeeeeeeee"
"fffggghhhhiiiiiijklllmmnnnnnnoooooooo"
"pppqrrrrrrssssssttttttttttttttuuuuuuv"
"wxyz ";

size_t alphasize = strlen(alphabet);

A quick function to get a pseudorandom number:

int getrand(int low, int high)
{
return (high + 1 - low) * (rand() / (RAND_MAX + 1.0)) + low;
}

Do a random letter like this:

offset = getrand(0, alphasize - 1);
putc(alphabet[offset]);

in a loop, obviously. Including spaces in the "alphabet" makes word
separation a cinch, but you might want to be a bit cleverer than that to
avoid big gaps between some w ords. (A simple flag will do the
trick.)

If, on the other hand, you want to have a list of words from which you print
random extracts, then the principle is exactly the same except that,
instead of an array of char, you have an array of char *, like this:

char *dictionary[] =
{
"now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "aid", "of", "party"
};

size_t dictionarysize = sizeof dictionary / sizeof dictionary[0];

apart from which the principle is the same.

> Is there any function that convert a number to it ascci char?
>
> My english is horrible! Here an example:
> function(65)
> return 'a';


Well, firstly C doesn't understand about ASCII - it uses your local
character collating sequence (which, in your case, is indeed ASCII, but not
everybody uses the same kind of computer that you do).

Secondly, 'a' is actually ASCII 97, not ASCII 65 (which is 'A').

Thirdly, C supplies no such function, because it isn't necessary.

Observe:

#include <stdio.h>

int main(void)
{
printf("The character with code point %d, on your", 65);
printf(" current set-up (but not necessarily other");
printf(" people's!), is %c\n, 65);

return 0;
}

In other words, on your computer at any rate, 65 and 'A' are just two
different ways of writing the same value. (On a mainframe using EBCDIC
rather than ASCII, 'A' would have some other numerical value.)

If you can avoid relying on ASCII values, it's better to do so as it makes
your code more portable. (It's very rare to have any /real/ need to rely on
ASCII in typical C code.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
arun
Guest
Posts: n/a
 
      05-02-2006

Olaf "El Blanco" wrote:
> How can i generate random words? ('a'..'z')
>
> Is there any function that convert a number to it ascci char?
>

Is it mandatory that you should make a function call there to do it ?
You can simply assign the number to a character or do a cast when
required.
That will do it.

> My english is horrible! Here an example:

Is it an example for your original english ?
> function(65)
> return 'a';
> I cant understand why the function returns 'a' for 65.

I believe 65 is the ASCII value of 'A' and your fuction should return
that.

> Thank you!


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-02-2006
arun said:

>
> Olaf "El Blanco" wrote:
>> How can i generate random words? ('a'..'z')
>>
>> Is there any function that convert a number to it ascci char?
>>

> Is it mandatory that you should make a function call there to do it ?
> You can simply assign the number to a character or do a cast when
> required.


Under which circumstances would a cast be required?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Olaf \El Blanco\
Guest
Posts: n/a
 
      05-02-2006
Richard Heathfield, Thank you! amazing explication!

> int getrand(int low, int high)
> {
> return (high + 1 - low) * (rand() / (RAND_MAX + 1.0)) + low;
> }


It works perfecty, but why RAND_MAX and why + 1.0?




 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-02-2006
Olaf "El Blanco" said:

> Richard Heathfield, Thank you! amazing explication!
>
>> int getrand(int low, int high)
>> {
>> return (high + 1 - low) * (rand() / (RAND_MAX + 1.0)) + low;
>> }

>
> It works perfecty,


Naturellement.

> but why RAND_MAX and why + 1.0?


Let's look at it in detail, then.

RAND_MAX is the highest legal value rand() can return. So if we divide the
result of rand() by RAND_MAX plus one, we'll get a result in, say, the
range 0 to 0.999+ (or, if you prefer, >= 0 and < 1).

But to do RAND_MAX + 1 would result in an integer addition with an integer
result, and dividing rand()'s return value by this would give us a result
of 0, which would get a bit repetitive after a while. So we use 1.0
instead, which makes (RAND_MAX + 1.0) a double, and thus dividing it into
rand()'s result is done using floating point arithmetic rather than integer
arithmetic.

That whole ball of wax gives us, as I say, a value in the range [0.0, 1.0) -
we will call this value R. Note the apparently mismatched brackets [) which
indicate that it is a "half-open interval" - i.e. the range includes all
the numbers from the lower value up to, but *not* including, the higher
value.

What we want is a result in the range [low, high]. The number of items in
the range is high + 1 - low. For example, if we want a number in the range
7 to 10, the acceptable answers are 7, 8, 9, 10 - i.e. four results, i.e.
high (10) - low (7) is one short, so we add 1 to the result of the
subtraction.

Multiplying this by our "random" value R gives us a number in the range [0,
high - low + 1). Adding low to this gives us a value in the range [low,
high + 1), which is almost what we want. At this point, though, it's still
a double, but since the function returns int, a conversion is supplied for
us - this conversion forces the value into the range [low, high], which is
exactly right.

HTH. HAND.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Olaf \El Blanco\
Guest
Posts: n/a
 
      05-02-2006


/*
This is an english translation of part of my code
In which I need to generate a lot of 1024 chars phrases
-MAX = 1024-
*/

char * phrase_generate(void)
{
char phrase[MAX];
int i, size_alphabet=strlen(alphabet);
for (i=0; i<=MAX; i++)
printf ("%c", phrase[i] = alphabet[getrand(0, size_alphabet-1)]);
return frase; /* Address of local var??? */
}

void testing(void)
{
char *phrase;
phrase =(char*)malloc(sizeof(char)*MAX);
phrase = phrase_generate();
printf ("\n\n\n");
printf ("%s", phrase);
free(phrase);
}

Don't appear the same phrases, the second one is shorter... Why?



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-02-2006
Olaf "El Blanco" said:

>
>
> /*
> This is an english translation of part of my code
> In which I need to generate a lot of 1024 chars phrases
> -MAX = 1024-
> */
>
> char * phrase_generate(void)
> {
> char phrase[MAX];
> int i, size_alphabet=strlen(alphabet);
> for (i=0; i<=MAX; i++)


You only have MAX characters, one of which is needed for a null terminating
character (which you appear to have overlooked completely). The loop,
however, runs from 0 to MAX, inclusive, which comes to MAX + 1 characters.
That's two too many, once you remember the null terminator.

> printf ("%c", phrase[i] = alphabet[getrand(0, size_alphabet-1)]);
> return frase; /* Address of local var??? */


(You meant phrase, not frase - you defined it as phrase, earlier in the
function.)

Yeah, don't do that - or, at least, the simplest fix is to make it static.
That'll work, although I happen to think it's a bug-ugly solution.

> }
>
> void testing(void)
> {
> char *phrase;
> phrase =(char*)malloc(sizeof(char)*MAX);


Lose the cast.

phrase = malloc(MAX * sizeof *phrase);

Don't forget malloc can fail, so:

if(phrase != NULL)
{

> phrase = phrase_generate();


Whoa, stop there. You just lost track of your malloc'd space. Instead, do
this:

strcpy(phrase, phrase_generate());

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Olaf \El Blanco\
Guest
Posts: n/a
 
      05-02-2006
Finally...

I will call that function getrand() X times...
And I need differents phrases... You talked about that, but I didn't
understand... Something like pascal randomise() ?
Thanks again...


 
Reply With Quote
 
Robert Latest
Guest
Posts: n/a
 
      05-02-2006
On 2006-05-02, Richard Heathfield <(E-Mail Removed)> wrote:

[ complete answer to homework question snipped ]

Since when are we doing this?

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Words Words utab C++ 6 02-16-2006 07:00 PM
Non-noise words are incorrectly recognised as noise words. Peter Strĝiman ASP .Net 1 08-23-2005 01:26 PM
Re: A little bit of help regarding my linked list program required. - "words.c" - "words.c" Richard Heathfield C Programming 7 10-05-2003 02:38 PM



Advertisments