Thread: Problem with random number

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    16

    Problem with random number

    I keep getting this error when trying to compile

    Code:
    int gen_rand(void) {
       int n;
       n = random(100);  /* n is random number in range of 0 - 99 */
       return(n);
    }
    Error
    Code:
    numguessgame.c: In function ‘gen_rand’:
    numguessgame.c:53: error: too many arguments to function ‘random’

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Have you included <stdlib.h>?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    And change random(100) to rand() % 100, or some other way to reduce the range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    Quote Originally Posted by laserlight View Post
    And change random(100) to rand() % 100, or some other way to reduce the range.
    thanks, it is now compiling, and generating a num, but its the same number everytime

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I think you need to seed it with srandom();

    You can do this with time for example like this

    Code:
    srandom((unsigned)time(NULL));

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    srand(), not srandom(). More generally, read Prelude's article on Using rand().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Why not srandom()? The random() man page seem to suggest that srandom goes with random.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Subsonics
    Why not srandom()? The random() man page seem to suggest that srandom goes with random.
    random() and srandom() are non-standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by laserlight View Post
    random() and srandom() are non-standard.
    Ok, but random is what is used in the code example though.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Subsonics
    Ok, but random is what is used in the code example though.
    That is true, and looking at the error message again, I see that it was declared, so it is possible that ~C_Student~ is using the random() that you have in mind and thus srandom() would be appropriate, but we cannot tell from the given information.

    Basically, if you are trying to use a function outside of the C standard library, then you should state what exactly it is that you are trying to use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. problem with random numbers
    By xxwerdxx in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 08:56 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 08:04 PM
  5. Random Problem?
    By dizz in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2002, 05:00 PM