0

I want to fill an array with random integers from -10 to 10. My function looks like this:

int array(int a[], int n, int min, int max){

for(int i=0;i<n;i++){
    a[i]=(rand()%min);
    printf("%d ", a[i] );
}   

In main I have declared "min" to -10. But it does not really work, it still prints between 0 - 9. I have tried with +1 after min, then i get 10, but still no negative integers.

Laros
  • 3
  • 3
  • 1
    [This answer](https://stackoverflow.com/a/14598879/4142924) shows how to obtain a random number within a given range. – Weather Vane Sep 15 '22 at 18:57

3 Answers3

1

The number of possible number is max - min + 1, so that's what you want to mod by, giving you a number from 0 to max - min. Then you add min to that to give you a number from min to max.

a[i]=(rand() % (max-min+1) + min );
dbush
  • 205,898
  • 23
  • 218
  • 273
  • This worked. Thank you so much! I just want to make sure I undertand this. Lets say random number is 55 and min= -10 max=10. 55 % (10-(-10)+1) +(-10). 55 % 21 equals to 13 then - 10, will it equal to 3? Do I understand it or is it completely wrong? – Laros Sep 15 '22 at 19:08
1

The % (modulus) operator divides two integer types and returns the remainder, which may be negative if the left operator is negative. However rand() does not return negative values. This is why no negative numbers are getting inserted. Try:

a[i] = (rand()%21) - 10; 

Also, make sure to return a value from your function. Perhaps int * makes more sense, if you're returning the array you generated.

  • 1
    Please note that the result of `rand() % positive_value` will always be positive, because the value returned by `rand()` is in the range 0 to `RAND_MAX` inclusive (always positive). In general, the signess of `x % y` depends on the sign of `x` (see e.g. https://godbolt.org/z/Wjfss3148). – Bob__ Sep 15 '22 at 22:57
  • Thanks @Bob__ . Turns out google was wrong about this one. – Owen Phillips Sep 16 '22 at 03:50
  • That depends on what article you read ;). See e.g. https://stackoverflow.com/a/58768544/4944425 or https://stackoverflow.com/a/20638659/4944425 – Bob__ Sep 16 '22 at 08:11
1

Get a random number properly:

int random( int min, int max )
{
  int N = max - min + 1;
  int K = RAND_MAX - (RAND_MAX % N);
  int n;
  do n = rand(); while (n >= K);
  return (n % N) + min;
}

This function cannot give you random values in ranges with magnitude greater than RAND_MAX, and RAND_MAX is often fairly small (32,767). If you need a greater range, let us know.

Thereafter, apply the function to each element of your array:

void randomize_array( int a[], int n, int min, int max )
{
  while (n--) a[n] = random( min, max );
}

Keep separate operations separate. Randomizing an array has nothing to do with printing it. Print separately:

void print_array( int a[], int n )
{
  if (!n) return;
  printf( "%d", a[0] );
  for (int i = 1;  i < n;  i++)
    printf( " %d", a[i] );
  printf( "\n" );
}

Putting it all together:

#define sizeof_array(A) (sizeof((A)) / sizeof((A)[0]))

int a[20];

randomize_array( a, sizeof_array(a), -10, 10 );
print_array( a, sizeof_array(a) );
Dúthomhas
  • 8,200
  • 2
  • 17
  • 39