Random integer generation in given range Can my rand_int function be improved?

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;time.h&gt;

int rand_int(int a,int b)
{
    //The basic function here is like (rand() % range)+ Lower_bound

    srand(time(0));
    if (a &gt; b)
        return((rand() % (a-b+1)) + b);
    else if (b &gt; a)
        return((rand() % (b-a+1)) + a);
    else
        return a;
}

int main()
{
    int a,b;

    printf("\nEnter the range in which the a random no. is required: ");
    scanf("%d %d", &amp;a, &amp;b);

    printf("%d\n", rand_int(a,b));
}
First of all. Move srand() out of the function call. For now, it is not random at all if you manage to execute function twice in the same second. You will reset the random seed to the same value with time(0) as it normally has a 1s resolution.

Then I would suggest to do a conditional swap, and then perform "generate random" thing. This way you will split it into "preparation of arguments" and the "actual work parts".

You will have one exit point, which is a bit more readable and AFAIK tend to be a bit more optimizer friendly. Also, modern processors don't like too many ifs, especially unpredictable. So I would further suggest refactoring the "job" part into a separate function int rand_hi_lo(int upper, int lower) maybe even inlined. You could skip the branch at all then in certain cases.

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;time.h&gt;

void swap_int(int* a, int* b){ int tmp = *a; *a=*b; *b=tmp; }

inline int rand_int_hi_lo(int upper, int lower){
  return((rand() % (upper-lower+1)) + lower);
}

int rand_int(int a,int b)
{
    if (b &gt; a) swap_int(&amp;a,&amp;b);
    return rand_int_hi_lo(a,b);
}

int main()
{
    int a,b;
    srand(time(0));
    printf("\nEnter the range in which the a random no. is required: ");
    scanf("%d %d", &amp;a, &amp;b);
    printf("%d\n", rand_int(a,b));
}


It might be even possible to skip the conditional at all. But I am not sure about the negative modulo arithmetics. I think it is possible. Also my c is rusty, so I hope I wrote swap correctly.

EDIT:
I also skipped the a==b. I don't think it is worth to put there at all unless you are sure it happens more than rarely. If necessary I would put if (a==b) return a; it before the swap_int.