2

When I am trying to generate a random number 0 through int:

//Populate Currently Allocated Reasource for each customer
for (i = 0; i < NUMBER_OF_CUSTOMERS; i++) 
{
    printf("%d:[ ",i);
    for (j = 0; j < NUMBER_OF_RESOURCES; j++)
    {
        allocation[i][j] = rand() % maximum[i][j];
        printf("%d ",allocation[i][j]);
    }
    printf("] \n\n");
}

I get a floating point exception when maximum[i][j] is 0.

Are there any better means of creating a random number without the floating point error caused by rand() % 0?

EDIT: When maximum is 0, the random number output should just be 0.

Sean
  • 1,283
  • 9
  • 27
  • 43

5 Answers5

2

Taking a % b gives you the remainder when a is divided by b. As such, it will always give you a result that is less than b, and it doesn't work when b is zero, because you can't divide by zero.

If you want a random number between 0 and x, you need to take rand() % (x + 1).

  • What if maximum is negative? – codingEnthusiast Oct 17 '15 at 22:30
  • @naltipar Then things get weird. The behavior of the modulo operator with negative numbers is implementation-defined. http://stackoverflow.com/questions/7594508/modulo-operator-with-negative-values –  Oct 17 '15 at 23:21
1

Try to use this instead

//Populate Currently Allocated Reasource for each customer
for (i = 0; i < NUMBER_OF_CUSTOMERS; i++) 
{
    printf("%d:[ ",i);
    for (j = 0; j < NUMBER_OF_RESOURCES; j++)
    {
        allocation[i][j] = maximum[i][j] ? (rand() % maximum[i][j]) : 0;
        printf("%d ",allocation[i][j]);
    }
    printf("] \n\n");
}
Mykola
  • 3,343
  • 6
  • 23
  • 39
0

You said:

When maximum is 0, the random number output should just be 0.

You can add a check to take care of this.

if ( maximum[i][j] == 0 )
{
   allocation[i][j] = 0;
}
else
{
   allocation[i][j] = rand() % maximum[i][j];
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You can use the ?: operator to write that extra condition neatly:

allocation[i][j] = maximum[i][j] ? (rand() % maximum[i][j]) : 0;

?: Conditional Expression: If Condition is true ? Then value X : Otherwise value Y


In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.

Wikipedia

Community
  • 1
  • 1
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
0

When maximum is 0, the random number output should just be 0.

The modulo operator is the remainder after a division. Since division by zero is undefined, so is modulo 0.

You need a condition to prevent attempting to divide by zero. You can do that with if/else statements, or the ternary operator (?:). In other words, change this

allocation[i][j] = rand() % maximum[i][j];

to one of the following.

if (maximum[i][j] == 0)
{
    allocation[i][j] = 0;
}
else
{
    allocation[i][j] = (rand() % maximum[i][j]);
}

or

allocation[i][j] = (maximum[i][j] == 0) ? (rand() % maximum[i][j]) : 0;
cp.engr
  • 2,291
  • 4
  • 28
  • 42
  • Of course, for better or worse, the condition `(maximum[i][j] == 0)` can be further abbreviated due to `maximum[i][j]`, since 0 evaluates as false in C. But I think the explicit condition is more clear. – cp.engr Oct 17 '15 at 21:40