?
Solved

How to use rand to generate a four-letter word?

Posted on 2000-03-07
5
Medium Priority
?
253 Views
Last Modified: 2010-04-15
Thanks!
0
Comment
Question by:huhulucy90
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:ibo
ID: 2593458
try this.. not very clever but i think this will work :)

char word[5]="";

....

randomize();
word[0] = random(25) + 1 + 95;
word[1] = random(25) + 1 + 95;
word[2] = random(25) + 1 + 95;
word[3] = random(25) + 1 + 95;
word[4] = '\x0';

printf("word = %s\n", word);

....


btw, inlcude stdlib and time libraries.
magic number 95? thats for lower case
65 for upper case (i think).
0
 
LVL 3

Accepted Solution

by:
ibo earned 200 total points
ID: 2593491
oh oh.. rand()

make it somethin like

word[i] = (int) (rand() % 25) + 97;

correction : 97 not 95 :/
0
 
LVL 84

Expert Comment

by:ozo
ID: 2593650
you can also use 26 instead of 25
0
 

Author Comment

by:huhulucy90
ID: 2593776
Thank you all for your help!
I've got the output like:
word = qmzr

0
 

Author Comment

by:huhulucy90
ID: 2593823
and I also add a sentence like:
srand(time(NULL));
so get a different output each time.
cheer!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question