Random Numbers...Problem With FAQ Answer

This is a discussion on Random Numbers...Problem With FAQ Answer within the C++ Programming forums, part of the General Programming Boards category; Hi all, I've just tried testing the random numbers answer in the FAQ ( http://faq.cprogramming.com/cgi-bin/...&id=1043284385 ) and it isn't working ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Random Numbers...Problem With FAQ Answer

    Hi all,

    I've just tried testing the random numbers answer in the FAQ (http://faq.cprogramming.com/cgi-bin/...&id=1043284385) and it isn't working for me. I've got the exact same C++ code shown there:

    Code:
    #include <iostream> 
    #include <ctime> 
    
    int main(void)
    {
      int i, r;
    
      for (i = 0; i < 20; i++)
      {
        r = GetRand(10, 12);
        std::cout <<"Your number is " <<r <<std::endl;
      }
    
      return(0);
    }
    All that happens is that line ten is highlighted, with the error "'GetRand' undeclared (first use this function)".

    It's probably a real simple answer, but I've just started learning C++, so could anybody be kind enough to help?

    Thanks very much,
    Mike

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Try using the <cstdlib> header file .
    I only know srand so i could be wrong.
    And your return 0; is mistake also.
    take away the () sourounding the 0;

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Hmm... still gives the same error message, thanks anyway blackgold

    Anyone got any ideas?

    Thanks a lot,
    Mike

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    They just showed you what needed to change. You still need the function declaration from the C-code.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >still gives the same error message
    Is that all of the code in your program? If so, it's no surprise that the function isn't found as you don't define it. Your code should look something like this:
    Code:
    #include <iostream> 
    #include <ctime>
    #include <cstdlib>
    
    int GetRand(int min, int max);
    
    int main(void)
    {
      int i, r;
    
      for (i = 0; i < 20; i++)
      {
        r = GetRand(10, 12);
        std::cout <<"Your number is " <<r <<std::endl;
      }
    
      return(0);
    }
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        std::srand(std::time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      rc = (std::rand() % (max - min + 1) + min);
      
      return (rc);
    }
    >And your return 0; is mistake also.
    No it isn't. You can enclose an expression with parentheses, they don't do anything in this case and are perfectly legal.
    My best code is written with the delete key.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can leave () around the 0 on return, its prefectly fine.
    All that happens is that line ten is highlighted, with the error "'GetRand' undeclared (first use this function)".
    Did you remember to copy GetRand from the example above it?

    The example you copied was just showing how to change it to a c++ document.
    Code:
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        srand(time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }
    Edit: Foiled!

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Oh thanks also for the () around ;0 what is the purpose of doing this i wonder?
    Thanks alot.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what is the purpose of doing this i wonder?
    Ancient C used to require them, but not for long. The people that learned C at the time continued to use them out of habit, and created confusion. These days it's pretty well known that parentheses aren't required for a return statement, but some people still use it as a matter of style. They feel that it keeps their syntax more consistent.
    My best code is written with the delete key.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >They feel that it keeps their syntax more consistent.
    Yep. Them old people go through a lot of trouble to stay regular.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Ok thanks alot ,old folks

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Them old people go through a lot of trouble to stay regular.
    What an awful pun. I wish I'd thought of it.
    My best code is written with the delete key.

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I guess I'll take that as a compliment...
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Yup works now. Said it would be something simple

    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 02:22 PM
  3. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 03:01 PM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21