0

I have only used the function twice and it displays the aforementioned error. Can someone explain as to why the compiler does that?

void printrandom()
{
    int x = (rand(5)+1);
    int y = (rand(5)+1);
    printf("%d and %d - a total of %d", x, y, (x+y));
}
user3238099
  • 21
  • 1
  • 2

2 Answers2

2

It is actually rand(void), which is why you are getting that error.

Try int x = (rand() % 5) + 1;

EDIT as Daniel points out, using % will actually affect the probability. See his link for how to address this issue.

Community
  • 1
  • 1
schumacher574
  • 1,050
  • 1
  • 15
  • 31
  • -1. Using `rand() % N` is flawed and should be avoided. See [this question](http://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator) to find out why. – Daniel Kamil Kozar Feb 09 '14 at 18:56
  • @DanielKamilKozar; Good link. Thanks :) – haccks Feb 09 '14 at 19:00
  • 4
    @DanielKamilKozar: If the bias introduced by `rand() % 5` is a significant problem, then you should probably be using something better than `rand` anyway. – Keith Thompson Feb 12 '14 at 21:15
0

Declaration for rand() function is

int rand(void);  

This means that it takes no arguments. Remove 5 from rand. If you want to generate random numbers from 1 to 5, the you can do this as

int x = rand()%5 + 1;  
haccks
  • 100,941
  • 24
  • 163
  • 252