# How do I make random number with precision setting

0 favourites
• 3 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
• Hi,

I would really appreciate it if someone gave me an idea on how to achieve the following relation:

I have a target number, say X.

Now I get a random number Y.

And I have a "precision" skill/setting that can be set from 0 onwards.

Basically if the precision is 0, Y is pretty much just random, but the more I increase it, the closer it is likely to be to X.

For example, if X=100 and the precision is set to 0, you can get anything like 250, 180, 20, 150 or 432.

If the precision is at 50, then you get closer random values like 120, 90, 85, 110.

And if it's at 100, you get almost perfect ones 101, 99 or 98 or 100.

In other words, the greater precision, the smaller the random range of Y around the target number.

Also would be good if it was a cubic function so that the improvement in precision gets smaller with higher settings, never reaching perfection.

Any ideas? :)

• I can think of 2 ways to do this, both are quite mathmatically simple.

First option is to add a random number to your target value.

`target + (random(1) - 0.5) * (100 - precision)`

For the above it assumes precision is in the range 0 to 100, and gives an output between target ± 100 and target ± 0. If you want 100% precision to not match exactly then you can do

`target + (random(1) - 0.5) * (100 - precision * 0.95)`

target ± 100 to target ± 5

If you want a larger range

`target + (random(1) - 0.5) * (100 - precision) * 4`

target ± 400 to target ± 0

Second option is to generate a random number and do a linear interpolation towards your target value.

`lerp(random(100), X, precision * 0.01)`

target ± 100 to target ± 0

If you want to change the range then just modify the number given to random, if you want 100% to not be exact try

`lerp(random(100), X, precision * 0.0095)`

target ± 100 to target ± 5

One thing you can do with this one is change the probably curve, by performing a power on the precision value like so

`lerp(random(100), X, (precision * 0.0095) ^ 2)`

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• I can think of 2 ways to do this, both are quite mathmatically simple.

First option is to add a random number to your target value.

`target + (random(1) - 0.5) * (100 - precision)`

For the above it assumes precision is in the range 0 to 100, and gives an output between target ± 100 and target ± 0. If you want 100% precision to not match exactly then you can do

`target + (random(1) - 0.5) * (100 - precision * 0.95)`

target ± 100 to target ± 5

If you want a larger range

`target + (random(1) - 0.5) * (100 - precision) * 4`

target ± 400 to target ± 0

Second option is to generate a random number and do a linear interpolation towards your target value.

`lerp(random(100), X, precision * 0.01)`

target ± 100 to target ± 0

If you want to change the range then just modify the number given to random, if you want 100% to not be exact try

`lerp(random(100), X, precision * 0.0095)`

target ± 100 to target ± 5

One thing you can do with this one is change the probably curve, by performing a power on the precision value like so

`lerp(random(100), X, (precision * 0.0095) ^ 2)`

Yes, thanks! That's exactly the kind of answer I was looking for.

It actually gets slightly more complicated because in practice I need to do this for a set of coordinates (no big deal, I'll just do it twice).

I did think about the #1 solution with subtracting from 100 before, but it was a bit too linear, but the lerp stuff is great to tinker around with.

• 3 posts