Solved

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

Posted on 2000-02-28
5
172 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 76
GUI: DIalog Stacking and Popping in MS C++ 4 99
Issues with C++ Class 19 121
Gaming Software 1 39
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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