Solved

How can i generate a random no within 100????

Posted on 2000-02-28
5
170 Views
Last Modified: 2010-05-18
Hi !

                           I used rand()  fn to generate a random no.How can i define the max limit for that....there
                   is  RAND_MAX....but i want to override that....

                   Can anyone pls help.....

                   Thanks in advance.
0
Comment
Question by:ceena
5 Comments
 

Accepted Solution

by:
ecebhume earned 50 total points
ID: 2567735
You don't need to override the RAND_MAX, all you need to do is once you get a random number, you mod it with 100, you'll get integer between 0 and 99 (add 1 to get b/w 1 and 100) or mod 101 to get b/w 0 and 100

ie:
// assume you do all initialize stuff up here with srand already
x = rand();

random_number = x % 100; // this gives you integer, 0 <= random_number <= 99

ecebhume
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2567803
If u want to increase the range of Random Numbers then u can use srand() function from Stdlib.h
or If u want to give ur own value then try opening stdlib.h and modify the value of RAND_MAX in it. Im not Very Sure of second thing Just Check it.
All the Best
0
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2567895
Changing the value of RAND_MAX is a very bad idea.
Go with ecubhume's solution
Luc
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2567918
Follow ecebhume's advice, wrap it in a small function:
  inline int
  random(int n)
 {
    return rand() % n;
 }
0
 

Author Comment

by:ceena
ID: 2567983
Thanks all.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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