C++/plz help

Advertisement


Question
QUESTION: hi
can you give me a code wrote with rnd function for choosing a number between 1-10?
i really get confused with this function, and really need your help
thanx
Bita

ANSWER: rand() returns an integer between 0 and RAND_MAX (inclusive). the constant RAND_MAX is implementation dependant.

the sequence of pseudo-random numbers returned by calls to rand() is dependent on the initial value, called a seed. the default seed is 1; the sequence of random numbers generated will be the same on every run of the program.

srand() allows us to specify the initial seed value used by rand. a reasonably arbitrary seed value can be obtained from the always-changing value of the system clock returned by the function time(). this worlks well if the system clock has a reasonably high resolution.

on many rand() implementations, the lower-order bits returned are much less random than the higher-order bits. one should be careful about using this function in applications where good randomness is needed.

we can generate a random integer between 1 and 10, using high-order bits, as follows:

#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
      using namespace std ;

      srand( unsigned( time(0) ) ) ; // seed with time from the system clock

      // generate and print 5 pseudo-random numbers between 1 and 10
      for( int i=0 ; i<5 ; ++i )
      {
         int random_number = 1 + int( 10.0 * ( rand()  /  ( RAND_MAX + 1.0 ) ) ) ;
         cout <<  random_number << '\n' ;
      }
}



---------- FOLLOW-UP ----------

QUESTION: thanks for your answering...
you said that "rand() returns an integer between 0 and RAND_MAX "it means that RAND_MAX is 10 in this code that you wrote?
and if it is right why we dont write 10 instead of RAND_MAX in this line below?

int random_number = 1 + int( 10.0 * ( rand()  /  ( RAND_MAX + 1.0 ) ) ) ;

and in loop that you wrote it finds a number between 0 and 10,for 5 times?right?

thanx
Bita

Answer
rand() returns an integer between 0 and RAND_MAX (inclusive).
the constant RAND_MAX is implementation dependant (and will typically be a large integer value).

rand() generates an integer in the inclusive interval [ 0, RAND_MAX ].
if, on an implementation, RAND_MAX is 1073741824,
rand() would generate a number n such that 0 <= n <= 1073741824

rand()  /  ( RAND_MAX + 1.0 ) would give a double value in the range [ 0.0 , 0.99999999... ].

10.0 * ( rand()  /  ( RAND_MAX + 1.0 ) ) would give a double value in the range [ 0.0 , 9.9999999... ].

int( 10.0 * ( rand()  /  ( RAND_MAX + 1.0 ) ) ) would give an integer in the range [ 0 , 9 ].

and 1 + int( 10.0 * ( rand()  /  ( RAND_MAX + 1.0 ) ) ) would give an integer in the range [ 1 , 10 ],
which is what we want.

> and in loop that you wrote it finds a number between 0 and 10,for 5 times?right?
right.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.