0

So I am currently learning C, and I have some confusion about how the random() function works. I know I have to provide a seed, but I don't know how to actually generate the random number.

srandom(seed);
long int value= random(40);

When I try this it gives me a compiler error:

too many arguments to function ‘long int random()

The end goal is to get a random number between 1 and 40. I should add that the goal is to not use the rand() function.

3
  • 3
    random doesn't take arguments; you passed the value 40. Commented Feb 25, 2017 at 18:17
  • Please do not place srand() before each call to rand(). Call it once at the start of the program. I do not know why you renamed those library functions. What are they? How can we know what they do? Are they non-standard functions or your own? Commented Feb 25, 2017 at 18:22
  • Read the POSIX specification for random() — it tells you a lot of what to do. For the rest, read Is this C implementation of Fisher-Yates shuffle correct? to see how to generate an unbiassed random number in the range 0..N (for 1..40, you'll use a random number in the range 0..39 and add 1). Commented Feb 25, 2017 at 21:34

3 Answers 3

6

The answer to your question is precisely answered by the compiler. You are passing '40' to the function random() which it is not supposed to receive.

The signature for random() is:

long int random (void) 

void keyword here is indicator that the function in hand is not supposed to receive anything.

Here's an example program to generate 6 random numbers.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    int max = 5; 
    for (int c = 0; c <= max; c++){
      num = random();
      printf("%d\n",num);         
   }
   return 0;
}

Output:

fire-trail@Sahai-Yantra:~$ gcc try.c 
fire-trail@Sahai-Yantra:~$ ./a.out
1804289383
846930886
1681692777
1714636915
1957747793
424238335

To reach your end goal of generating random numbers within a given range using random(), see How to generate a random number from within a range.

Sign up to request clarification or add additional context in comments.

7 Comments

So far, so good. Now he wants to generate a number in the range 1..40; how do you go about doing that?
OK; so update your answer to reference the question, or include an outline of the information and the cross-reference to its source.
@JonathanLeffler is this okay? Or should I add up anything? newbie here.
I recommend using the link's title rather than an uninformative 'follow this link'. The latter looks like spam, even though it isn't. I've updated as I suggest. I might avoid using the bold for 'end goal' and I'd probably use back-ticks around random() so it appears in constant-width font, but those are second-order refinements.
|
3

The answer is right there in the error thrown by the compiler

too many arguments to function ‘long int random()

Change random(40) to random()

Comments

2

There is no argument in random() function.

See man page of random(). Prototype of random()

long int random(void);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.