Random Interger

Dimlos

Well-known member
The RANDOM() function does not allow you to specify minimum and maximum values in an easily understandable way.
That is why I use awk to create them.
The local variables min and max are the magic text when the shell script is executed.
 

Endercraft

Moderator (& bug finder :D)
RANDOM([lv=dic1],[lv=dic2])
This expression will not work because random does not accept any arguments and generates a decimal between 0 and 1. (I know @Dimlos explained it but I had another thing to do which delayed my reply)
If you just want to specify the minimum and maximum values with magic text, my macro achieves that.
@OscariBot searches for a simple workaround :
Thanks for all ur contributions... But all I needed is a simple random integer generator.
I got stuck with the same problem recently, so I used an (online) API to resolve this. I didn't use random.org because the one I found was simpler.
Screenshot_2023-02-25-11-32-38-916-edit_com.arlosoft.macrodroid.jpg
You can specify the number of numbers that you need. I used 1, so because the returned string is [number], I used a text manipulation to extract the number (\d).
 

Dimlos

Well-known member
After all, the standard MacroDroid functions cannot easily generate them, so we have to use shell scripts or APIs.
 

Dm114

Well-known member
Like i said earlier am looking for a very simple way to generate random integer numbers between lv1 and lv2
The local variables contain the min and max numbers.

I hop u got a clear explanation.

By the way wat is the use of the RANDOM () Expression?

I thought I could use the expression below to generate random numbers between 1 and 12
RANDOM([lv=dic1],[lv=dic2])
The local variables hold the min and max number and ofcuz they are intergers
I don't understand what you are all looking for...

1⁰) As a fact, RANDOM() function has NO arguments and generates a new number 0≤ d <1 evey time it is executed
2⁰) As I told previously, to get a random number in the range 1≤ n ≤ max use this formula RANDOM()*[lv=max])+1

So, to get such a number between min and max, the formula is FLOOR(RANDOM()*([lv=max]-[lv=min]+1))+[lv=min]

That's it.
 

OscariBot

Active member
Oh thanks guys... I prefer to use @Dimlos method, it looks simple... Using an API sometimes slow processing speed due to poor network.
@Dimlos am gonna try this expression now, I hope it works.... I will b bak to say thank you.
 

Dm114

Well-known member
Oh thanks guys... I prefer to use @Dimlos method, it looks simple... Using an API sometimes slow processing speed due to poor network.
@Dimlos am gonna try this expression now, I hope it works.... I will b bak to say thank you.
Dimlos' formula is the one I gave you previously to simply get a random number from 1 to max (see below)
Use this Set variable action
(RANDOM()*[lv=max])+1

Now, look at the one I gave you a few minutes ago to get a random number between whatever boundaries...
 

OscariBot

Active member
If the minimum value is 1, there is no need to specify it, if the maximum value is dic2, it is like this.
(RANDOM()*[lv=dic2])+1
Thanks @Dimlos .... It works perfectly, though it seems not to b a perfect random number generator... I ran it up to 20 times and 8 and 2 was missing... I repeat again for another 20times and 8 and 2 still never show up... Hahaaa but it's better than non bro afterall it's a work around.

Thanks once more
 

Dimlos

Well-known member
@Dm114
I certainly may not have understood enough about the RANDOM() function, but I don't think this is a simple math problem, I waited a day and finally tried the answer that came up and it did indeed work.
 

Dimlos

Well-known member
I don't see much difference between using functions and shell scripts.
However, mine works fast even if I call it continuously in a short period of time because of the countermeasure.

Random.mp4
 

OscariBot

Active member
@Dm114
Please watch the video I uploaded, and you will see that RANDOM() does not help in certain environments.
Oh I observe from the video that using shell script gives absolute random number 10times... But the latter seems to latch with just one figure through the 10times.
...but don't you think adding delay in d loop will solve this issue?
 

Dimlos

Well-known member
@OscariBot
In most cases, this will not be a major problem, as you do not need the faster processing described in the video.
If faster processing is needed, shell scripting is required.
 

Dm114

Well-known member
@Dm114
Please watch the video I uploaded, and you will see that RANDOM() does not help in certain environments.
I know random() function is not perfect. Mainly when the interval is narrow.

I was only promoting a bit integrated functions instead of more sophisticated solutions which are sometimes complicated to maintain or to understand.

As I'm not familiar with shell script, I don't know why your solution gives obviously better results as integrated random() function because I guess it's not a MD specific one. I suppose MD makes calls to internal functions. Let's ask MD Dev...

As a comparison, you'll find below a quick test I made. It seems that the results are a bit better when adding a delay between 2 random() function calls.
 

Attachments

  • Screenshot_20230227-140458_MacroDroid.jpg
    Screenshot_20230227-140458_MacroDroid.jpg
    488 KB · Views: 16

Dimlos

Well-known member
@Dm114
Your posts have helped me learn more about the RANDOM() function, and I think most people would choose your solution, which gives results in one action.
I just wanted to let you know that my solution also has its advantages.
 

OscariBot

Active member
I know random() function is not perfect. Mainly when the interval is narrow.

I was only promoting a bit integrated functions instead of more sophisticated solutions which are sometimes complicated to maintain or to understand.

As I'm not familiar with shell script, I don't know why your solution gives obviously better results as integrated random() function because I guess it's not a MD specific one. I suppose MD makes calls to internal functions. Let's ask MD Dev...

As a comparison, you'll find below a quick test I made. It seems that the results are a bit better when adding a delay between 2 random() function calls.

Addition... Am really still wondering how this RANDOM() worked.... it seems it value only updated when something happen to your phone....eg I noticed that staying on one screen and repeating the macro continue to give same value, but once u flip the screen or perform any gesture it value change... It also seems its updating value too using our phone time as a trigger... eg when minute or sec changes... Sometimes it's the intensity of the music playing in the phon.. infact it seems it's watching over for something to happen before it updated.

Am still playing with it to really figure it out.
 
Top