Results 1 to 4 of 4

Thread: Compile time error while generating Random Number in C++

  1. #1
    Join Date
    Apr 2009
    Posts
    68

    Compile time error while generating Random Number in C++

    Hello Programing Gurus,

    I am doing C++ practice at my own by going through either by the Internet or the MSDN library. I am developing an application in which I wanted to generate the random number in the range between 0 to 200 which should be exactly multiples of 3 and the list of number should not be more than 10.

    I know this is very simple program, I am also successful in developing the logic behind it but not able to implement it clearly.

    So please help me providing some focus on it.

    Thanks

  2. #2
    Join Date
    Dec 2008
    Posts
    202

    Re: Compile time error while generating Random Number in C++

    There are two different methods of creating random number within a given range. The first one is typical of most random number generators and simply uses a call to rand() and a modulus operator to place the random numbers within a range. The second is more advanced and returns a random number which is chosen by shuffling the elements of a vector which only contains values that exist within the specified range.

    Please follow the zip file for example:

  3. #3
    Join Date
    Jan 2009
    Posts
    140

    Re: Compile time error while generating Random Number in C++

    The ability to generate random numbers can be useful in certain kinds of programs, that need to model random events.In real life, we often generate random results by doing things like flipping a coin, rolling a dice, or shuffling a deck of cards. These events involve so many physical variables (eg. gravity, friction, air resistance, momentum, etc…) that they become almost impossible to predict or control, and produce results that are for all intents and purposes random.

    A pseudo-random number generator (PRNG) is a program that takes a starting number (called a seed), and performs mathematical operations on it to transform it into some other number that appears to be unrelated to the seed. It then takes that generated number and performs the same mathematical operation on it to transform it into a new number that appears unrelated to the number it was generated from.

    Code:
    # #include <stdafx.h>  
    # #include <iostream>  
    # using namespace std;  
    #   
    # unsigned int PRNG()  
    # {  
    #     // our initial starting seed is 2  
    #     static unsigned int nSeed = 2;  
    #   
    #     // Take the current seed and generate a new value from it  
    #     // Due to our use of large constants and overflow, it would be  
    #     // very hard for someone to predict what the next number is  
    #     // going to be from the previous one.  
    #     nSeed = (2 * nSeed + 50);  
    #   
    #     // Take the seed and return a value between 0 and 200 
    #     return nSeed  % 200;  
    # }  
    #   
    # int main()  
    # {  
    #     // Print 100 random numbers  
    #     for (int nCount=0; nCount < 200; ++nCount)  
    #     {  
    #         cout << PRNG() << "\t";  
    #   
    #         // If we've printed 5 numbers, start a new column  
    #         if ((nCount+1) % 3 == 0)  
    #             cout << endl;  
    #     }  
    # }

  4. #4
    Join Date
    Jan 2009
    Posts
    143

    Re: Compile time error while generating Random Number in C++

    C (and by extension C++) comes with a built-in pseudo-random number generator. It is implemented as two separate functions that live in the cstdlib header:

    srand() sets the initial seed value. srand() should only be called once.

    rand() generates the next random number in the sequence (starting from the seed set by srand()).

    Here’s a sample program using these functions:

    Code:
       1. #include <stdafx.h>  
       2. #include <iostream>  
       3. #include <cstdlib> // for rand() and srand()  
       4. using namespace std;  
       5.   
       6. int main()  
       7. {  
       8.     srand(2); // set initial seed value to 2  
       9.   
      10.     // Print 200 random numbers  
      11.     for (int nCount=0; nCount < 200; ++nCount)  
      12.     {  
      13.         cout << rand() << "\t";  
      14.   
      15.         // If we've printed numbers, start a new column  
      16.         if ((nCount+1) % 3 == 0)  
      17.             cout << endl;  
      18.     }  
      19. }

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Replies: 2
    Last Post: 09-10-2011, 10:15 PM
  3. How to use "Math.random()" to generate a random number in JavaScript?
    By Silent~Kid in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 05:06 AM
  4. Generating random number using library functions
    By Laquan in forum Software Development
    Replies: 3
    Last Post: 21-01-2010, 09:23 AM
  5. Date and time screwed up Error number: 0x80072F8F
    By whheezzzz in forum Windows Update
    Replies: 1
    Last Post: 26-03-2006, 04:41 AM

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,718,044,196.83654 seconds with 16 queries