Solved

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

Posted on 2000-03-07
5
241 Views
Last Modified: 2010-04-15
Thanks!
0
Comment
Question by:huhulucy90
  • 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 50 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now