Thread: too many arguments

  1. #1
    Registered User
    Join Date
    Jan 2009
    Nairobi, Kenya

    too many arguments

    am a beginer in C and
    i wanted to use'rand()' to generate random numbers, so i wrote the simple program:

    #include <stdio.h> /*defines scanf() and printf()*/
    #include <stdlib.h> //defines rand()
    #include <time.h> //defines time() and is needed for randomize()
    int main()
        //declaring a variable
        int n;
        n = rand(100);
        //display result
        printf("\nRandom numbers in the range 0-99");
       //wait for a key to be pressed by user before program exits
        }//end main
    but wen i compile i get a "too many arguments to function 'rand' " error. why is this?

  2. #2
    Registered User
    Join Date
    Jun 2005
    rand() accepts no arguments, and returns a single value. You've called it with one argument. Hence the error.

    Incidentally, call randomize() once and then call rand() multiple times (eg in a loop).

    There's also the incidental concern that randomize() is windows-specific. If you care about portability, use srand().

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    in front of my computer
    declaration for rand()
    int rand(void)

  4. #4
    Registered User
    Join Date
    Jan 2009
    Nairobi, Kenya
    i got it. i used:

    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h> 
    int main (int argc, char *argv[])
                /* Simple "srand()" seed: just use "time()" */
                unsigned int iseed = (unsigned int)time(NULL);
                srand (iseed);
               /* Now generate 5 pseudo-random numbers */
               int i;
               for (i=0; i<5; i++)
                printf ("rand[%d]= %u\n",
                i, rand ());
      return 0;
    }//end main
    and the output was:
    rand[0]= 1626340950
    rand[1]= 859402563
    rand[2]= 444565884
    rand[3]= 1614271775
    rand[4]= 403955761
    thanx, especially for the cross platform advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM