title: Generating a uniform distribution of INTEGERS in C
body: <p>I've written a C function that I think selects <strong>integers</strong> from a <strong>uniform distribution</strong> with range [rangeLow, rangeHigh], inclusive. This isn't homework--I'm just using this in some embedded systems tinkering that I'm doing for fun.</p>
  
  <p>In my test cases, this code appears to produce an appropriate distribution. I'm not feeling fully confident that the implementation is correct, though.
  Could someone do a sanity check and let me know if I've done anything wrong here?</p>
  
  <pre><code>//uniform_distribution returns an INTEGER in [rangeLow, rangeHigh], inclusive.
  int uniform_distribution(int rangeLow, int rangeHigh)
  {
      int myRand = (int)rand(); 
      int range = rangeHigh - rangeLow + 1; //+1 makes it [rangeLow, rangeHigh], inclusive.
      int myRand_scaled = (myRand % range) + rangeLow;
      return myRand_scaled;
  }
  //note: make sure rand() was already initialized using srand()
  </code></pre>
  
  <p>P.S. I searched for other questions like this. However, it was hard to filter out the small subset of questions that discuss random integers instead of random floating-point numbers.</p>