How to use Random() function

Ec116

New member
Hello dear all

Newbie here, Iam trying to get a random number from zero to a number the user has given.

So i have a user given number (for example the number 10) in an integer variable named UsrNum

How can i use the random() function to set a different variable (example RandNum) to get a random number from 0 to the user given number (ie 10)
 
Last edited:

Dm114

Well-known member
Hello dear all

Newbie here, Iam trying to get a random number from zero to a number the user has given.

So i have a user given number (for example the number 10) in an integer variable named UsrNum

How can i use the random() function to set a different variable (example RandNum) to get a random number from 0 to the user given number (ie 10)
Random() fonction returns a decimal number in the range of 0<d<1. So you can write something like: RandNum=round (UsrNum*random(), 0)
 

Ec116

New member
Thank you,

Jamie from macrodroid support helped me with the solution FLOOR(RANDOM() * [lv=UseNum] + 1)

What i proposed was adding the option to use either integer or decimal variables, for min and max, in the Random option
193469.jpg
 

Dm114

Well-known member
Thank you,

Jamie from macrodroid support helped me with the solution FLOOR(RANDOM() * [lv=UseNum] + 1)

What i proposed was adding the option to use either integer or decimal variables, for min and max, in the Random option
View attachment 224
As I suggested, you'd better use 'round' function rather than 'floor'. Otherwise you won't ever reach the upper limit...
 

MacroDroidDev

Administrator
Staff member
@Dm114 Please note the + 1 on the end of what I suggested. You should not use round if you want a true random distribution because for example if you have a max of 10, then only >= 9.5 will round to 10 (so this is only half as likely to every happen).
 

Dm114

Well-known member
@Dm114 Please note the + 1 on the end of what I suggested. You should not use round if you want a true random distribution because for example if you have a max of 10, then only >= 9.5 will round to 10 (so this is only half as likely to every happen).
You are right: sorry, I didn't notice the '+1'!
 

Fredroypoulin

New member
Would be nice to have arguments with the RANDOM Expression.

With integer variables, simply.

Example :

One argument :

RANDOM ( Variable_1 )
Gives you a number between 1 and Variable_1


Two arguments :

RANDOM ( Variable_1 , Variable_2 )
**Gives you a number between Variable_1 and Variable_2. (Like in the Random option but using variables instead of fixed values)**
 
Last edited:
Top