too many arguments

This is a discussion on too many arguments within the C Programming forums, part of the General Programming Boards category; am a beginer in C and i wanted to use'rand()' to generate random numbers, so i wrote the simple program: ...

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

    too many arguments

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

    Code:
    //libraries
    #include <stdio.h> /*defines scanf() and printf()*/
    #include <stdlib.h> //defines rand()
    #include <time.h> //defines time() and is needed for randomize()
    
    
    
    //main
    int main()
    {
        //declaring a variable
        int n;
    	randomize();
        n = rand(100);
    
        //display result
        printf("\nRandom numbers in the range 0-99");
    printf("%d",n);
    
       //wait for a key to be pressed by user before program exits
    getch();
    
        }//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
    Posts
    5,826
    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
    Location
    in front of my computer
    Posts
    804
    declaration for rand()
    Code:
    int rand(void)

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

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h> 
     
    //main
    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, 01:11 PM

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