Solved

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

Posted on 2000-03-07
5
242 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pgpool-II on Ubuntu 14.04... ARGH! 5 841
voltage to force translation ? 8 97
Compile VxWorks Toronado project under Visual Studio 11 180
Coverting 24 hour time to 12 hour in C++ 15 168
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

912 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

17 Experts available now in Live!

Get 1:1 Help Now