[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2000-03-07
5
Medium Priority
?
257 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 200 total points
ID: 2593491
oh oh.. rand()

make it somethin like

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

correction : 97 not 95 :/
0
 
LVL 85

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

865 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