Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2000-02-28
5
Medium Priority
?
174 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
[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
5 Comments
 

Accepted Solution

by:
ecebhume earned 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

670 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