Search code examples
armkeil

Scaling a 32 bit random number to get it into specific range?


In ARM assembly how does one go about scaling a 32 bit random number to get a number between 4.0-25.0?

All I can think of is extracting first five bits and work with that but that is not really scaling...

I'm looking for something like multiplying and dividing the 32 bit number to get it into that range.


Solution

  • The technique you want to implement is something like this (in C):

    float scale (u32 v)
    {
        return  (float) v / 0xffffffffL * (25 - 4) + 4.0;
    }
    

    Since the denominator is constant, it could be rewritten using only multiplication, if speed is important.