Results 1 to 5 of 5

Thread: What can be the use of the rand() of C++

  1. #1
    Join Date
    Dec 2009
    Posts
    28

    What can be the use of the rand() of C++

    Hello, Everybody. I am learning the C++ language. So, I do not know much more about the C++ language. Up till now I have learned how to use the functions and what can be the use of the functions in any program. So, I would like to know about the rand() function of the C++ language. Also, tell me how to use the rand() function in the C++ language. Reply Me!!!

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    The use of the rand() of C++

    The rand() function of the C++ language can be used to produced the random numbers. The rand() function can returns a pseudo-random integral number in the range of 0 to RAND_MAX. These random numbers can be generated by an algorithm. This an algorithm can uses a seed to produce the series. The following can be the syntax of the rand() function as :
    Code:
    int rand ( int );

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    What can be the use of the rand() of C++

    The programming code listed below can demonstrates you about the rand() function of the C++ language :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main ()
    {
      int iScrt, iGss;
      srand ( time(NULL) );
      iScrt = rand() % 10 + 1;
      do
     {
        printf ("Guess the number (1 to 10): ");
        scanf ("%d",&iGss);
        if (iScrt<iGss) puts ("The secret number is lower");
        else if (iScrt>iGss) puts ("The secret number is higher");
      } while (iScrt!=iGss);
      puts ("Congratulations!");
      return 0;
    }
    Output of the above code can be as follows :
    Code:
    Guess the number (1 to 10): 5
    The secret number is higher
    Guess the number (1 to 10): 8
    The secret number is lower
    Guess the number (1 to 10): 7
    Congratulations!
    In the above example, the random seed can be initialized to a value that can be representing the second in which the program can be executed. The time can be defined in the header file <ctime>. In this way to initialize the seed can be generally a good option for most of the random needs.
    Last edited by opaper; 02-03-2010 at 05:34 PM.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: What can be the use of the rand() of C++

    A typical way to produce the numbers that can be pseudo-random in a specified range using rand() can be to use the modulo of the value that can be returned by the range span and can adds the initial value of the range :
    ( val % 100 ) can be in the range 0 - 99
    ( val % 100 + 1 ) can be in the range 1 - 100
    ( val % 30 + 1985 ) can be in the range 1985 - 2014

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What can be the use of the rand() of C++

    The rand() function can returns a pseudorandom integer between zero and RAND_MAX. For example as :
    Code:
         rand( time(NULL) );
         for( j = 0; j < 10; j++ )
           printf( "Random number #%d: %d\n", j, rand() );
    You have to noticed that Do not use the modulus sign to limit the random numbers generated. The randomness can be heavily reduced. Inspite of use this algorithm to produce a proper distribution of random numbers between the 0 and other number :
    Code:
     int randomNumber(int h)
        {
           const float scl = rand()/float(RAND_MAX);
            return int(scl*h);
        }

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,518,851.69093 seconds with 15 queries