Math variables (random)

Hi Make

I’m making a scenario that posts a message at a given time in a Telegram channel. It is necessary to add a function that would insert a random number from 2.xx to 9.xx into the message, where “xx” is min 01, max 99. Step 0.01. For example, 2.00 / 2.01 / 3.15 / 4.09 / 9.99 etc.

Сan you tell me how this can be done :interrobang:

I understand the need to use the random function. But I don’t understand how to customize it for my case.

There is an example in the help center:

uuid-81fd5f8a-8050-beef-9184-deff3b944d95

I tried to use the suggested formula for the test

{{floor(random * (1.max - 1.min + 1)) + 1.min}}

Here’s what it looks like for me

But this formula cannot be tested, because an error.

2022-12-07_18-45-24

Hi @Rory ,

These “Min” and “Max” are variables declared earlier in that scenario via the module “Set Variables”. You would either need to do this as well, or just insert an integer like 1 - 6 in there.

1 Like

Hi @Bjorn.drivn

I’m understood, thank you. For the test, I inserted integers into the formula. Here’s what it looks like for me

Now I would like to understand how to improve this formula :thinking:

It is necessary to add a function that would insert a random number from 2.xx to 9.xx into the message, where “xx” is min 01, max 99. Step 0.01. For example, 2.00 / 2.01 / 3.15 / 4.09 / 9.99 etc.

In order to get a specific range of values first, you need to multiply by the magnitude of the range of values you want covered.

random * ( Max - Min )

This returns a value in the range [0,Max-Min), where ‘Max-Min’ is not included.

For example, if you want [5,10], you need to cover five integer values so you use

random * 5

This would return a value in the range [0,5], where 5 is not included.

Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.

Min + (random * (Max - Min))

You now will get a value in the range [Min,Max]. Following our example, that means [5,10]:

5 + (random * (10 - 5))

But, this still doesn’t include Max and you are getting a double value. In order to get the Max value included, you need to add 1 to your range parameter (Max - Min) and then round the decimal part to two decimal points. This is accomplished via:

Min + round(random * ((Max - Min) + 1);2)

And there you have it. A random integer value in the range [Min,Max], or per the example [5,10]:

5 + round(random * ((10 - 5) + 1);2)
2 Likes

Hello @alex.newpath

Thank you very much for the detailed information. It was useful to me.

But did it work for your situation?

No, it doesn’t work for my situation. But I can use it in my other scenario.

1 Like