Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

758 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

12 Experts available now in Live!

Get 1:1 Help Now