Thread: Generating random numbers between two inputs

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Generating random numbers between two inputs

    Hello,

    I am writing a program where a user enters two integers, a max and a min. From these two values, the program should generate 12 random numbers between those two values (inclusive). So if a user entered 10 and 22, then the answer can be : 10,11,12,13,14,15,16,17,18,19,20,21,22.

    Right now I am having some difficulty with just running the program. I am getting an error that says I have too few arguments to function random_generator on lines 20-31 [the block of printf statements]

    If someone can help me fix the error and make sure I am getting the random numbers it would be greatly appreciated. Thank you.

    Here is the code:

    Code:
    //Random
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    int random_generator(int minval, int maxval);
    
    int
    main(void)
    {
          srand(time(NULL));
          int minval, maxval;
          printf("Enter min and max integers.");
          scanf("%i", &minval);
          scanf("%i", &maxval);
          printf("Some random integers between %i and %i are:\n", minval, maxval);
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
          printf("%i\n", random_generator());
    
    
    }
          int random_generator(int minval, int maxval)
         {
          return (rand()%(maxval - minval )+ minval);
         }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You declared random_generator to take two arguments, so you must call it each time with the two arguments, e.g.

    Code:
    printf("%i\n", random_generator(10,22));
    By the way, the function as written will return a random value in the range [minval, maxval-1]. In other words, you will never get a random number that is as high as maxval from that function.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Quote Originally Posted by c99tutorial View Post
    You declared random_generator to take two arguments, so you must call it each time with the two arguments, e.g.

    Code:
    printf("%i\n", random_generator(10,22));
    By the way, the function as written will return a random value in the range [minval, maxval-1]. In other words, you will never get a random number that is as high as maxval from that function.
    Oh gotcha. Thanks!

    Is there some way I can get it to be [minval, maxval]?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    return rand() % (maxval - minval + 1) + minval;

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating Random Numbers
    By SterlingM in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2009, 07:01 AM
  2. Generating random numbers...
    By naspek in forum C Programming
    Replies: 7
    Last Post: 07-29-2009, 02:00 PM
  3. Generating Random Numbers
    By pizzapie in forum C++ Programming
    Replies: 17
    Last Post: 09-23-2004, 02:46 AM
  4. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 AM
  5. Generating Random Numbers
    By knight543 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2002, 06:55 PM