title: How to set rand() result to values in a certain range including negatives?
body: <p>I am trying to dynamically allocate an array of doubles and set each element to a random value within a range of positive or negative numbers, but I am having difficulty. </p>
  
  <p>Right now, I can only figure out how to set numbers 0 - max. </p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>double *random_arr(int size, double min, double max) {
      double *array0 = calloc(size, sizeof(double));
      if (array0 == NULL) {
              exit(1);
      }
      for (int i = 0; i &lt; size; i++)
              array0[i] = (max * rand() / RAND_MAX);
      return array0;
  }
  </code></pre>
  
  <p>My best guess:</p>
  
  <pre><code>for (int i = 0; i &lt; size; i++)
      array0[i]=((max + min) * rand() / RAND_MAX) - min;
  </code></pre>