Random number between X-Y

UPDATED-V1: Using @peternlewis suggestion to making the generation native to KM.

I need to choose a winner from a list in Excel. So I quickly made this macro to choose a random number between user defined values.

Keyboard Maestro 8.0.1 “Random number between X-Y” Macro

2 Likes

You can use the RAND function:

``````RandomX + RAND(RandomY-RandomX+1)
``````

2 Likes

Hello, I am not a professional KM user, and if it possible, would you like to help me? thanks. I need to generate some random number and automaticly type it as a name for a file to save it somewhere

Do you want it to happen when you have a file selected in the Finder?

Hello, Jimmy, I've already solve that idea, have a good day and thank you for your response.
Alex

Sorry, but someone already necro'd this and it's what showed up when I was searching for a solution...

I don't get the "RandomX + RAND(RandomY-RandomX+1)" bit.

Aren't we then doing (if we use default values)
1 + RAND(100-1+1)
1 + RAND(100)

Don't we want
1 + RAND(99)

RAND(99) will give you a random integer from 0 to 98, not 0 to 99.

Therefore 1+RAND(99) will give you a number from 1 to 99. I don't think that's what the original poster wanted.

When you want a random integer from X to Y, you have the same problem. Let's say X was 100 and Y was 200. Then 200-100 gives you 100 and RAND(100) gives you a number from 0 to 99 but when you add 100 to that you get a number from 100 to 199 not 100 to 200. Do you see why people add one now?

The problem is entirely different if we are dealing with real numbers. The KM Function "RANDOM(A,B)" returns a REAL number from A to B. The documentation says it will never return B, but it's still all the way up to B. So you don't really need to add 1 in the case of RANDOM(100,200). But for an integer from 100 to 200 you would have to say RAND(200-100+1)+100.

P.S. Nobody seems to mind when people necro on this site. And I like that because sometimes it's the best way to deal with things, in my opinion.