title: Transform random integers into range [min,max] without branching
body: <p>I got <a href="https://espadrine.github.io/blog/posts/shishua-the-fastest-prng-in-the-world.html" rel="nofollow noreferrer">hold on an SUPER-FAST algorithm</a> that generates an array of random bytes, uniformly. It's 6 times faster than c++ uniform distribution and mersenne-twister of std library.</p>
  <p>The count of an array is divisible by 4, so it can be interpreted as array of integers. Casting each entry to an integer, produces values in the range <code>[INT_MIN, INT_MAX]</code>. But how can I transform these integer values to lie between my own <code>[min, maximum]</code>?</p>
  <p>I want to avoid any if-else, to avoid branching.</p>
  <hr />
  <p>Maybe I should apply some bitwise logic, to discard irrelevant bits in each number? (because all remaining, unmasked bits will be either 0 or 1 anyway). If I can extract the most significant bit in my maximum-value, I could mask any bits that are more significant than that one, in my integers.</p>
  <p>For example, if I want my <code>max</code> to be 17, then it is <code>00010001</code> in binary form.  Maybe my mask would then look as <code>00011111</code>? I could then apply it to all numbers in my array.</p>
  <p>But, this mask is wrong ...It actually allows values up to <code>(1+2+4+8+16)</code> :(</p>
  <p>What can I do? Also, how to take care of the <code>min</code>?</p>
  <p><strong>Edit</strong></p>
  <p>I am generating millions of numbers every frame of my application, for neural networks. I managed to vectorize the code using AXV2 for float variants (using <a href="https://stackoverflow.com/a/65529900/9007125">this post</a>), but need to get integers working too.</p>