#### how to scale a number in a range of [min , max] [closed]

basically what I want is a way to scale any number no matter how big it is or whether it is signed or unsigned, to fit between a given range.

this is not specific to my code or to my coding language, but I’ll provide a sample of what I’m trying to achieve:

``````this code is meant to generate a random number using " linear congruential generator "
either by taking a seed or by taking a seed and a range between min and max :

int seed = time(0);
unsigned int multiplier = 48271;
unsigned int increment = 0;
unsigned long long modulus = (2 * pow(10,31)) - 1;

...

int getRandom(int Givenseed)
{
return (multiplier * Givenseed + increment) % modulus;
}

int getRandom(int min, int max,int Givenseed)
{
int result = (multiplier * Givenseed + increment) % modulus;

non of the ways below seems to work :

//return result % (max - min + 1);
//return min + (result / (modulus - 1)) * (max - min);
//return (result * (max - min)) + min;
//return min + result * (max - min);
//return (result % (max - min + 1)) + min;
}

...
``````

as you can see everything is working just fine but when I get to working with the range things start to get messy, but as I said earlier this is just an example and what I really want is a way to scale a value to a range regardless of the context, the commented lines are basically what I found while searching but non of them seem to work

also I’m aware of the header in c++ and I’ve used it before, but this time I want to do all the work myself for the sake of experience

to simplify all of this my question in simply:

How do you scale a value into a range of two values using math ?

Source: Windows Questions C++