# How to get \$x to be random multiples of 24

## Recommended Posts

Hello,

I want \$x to be any multiple of 24 starting at 66, ie either 66, 90, 114, or 138

I can use the Mod function to get multiples of 24, but how can i get multiples of 24 that

are starting from 66?

Thanks for any ideas and/or for code example that gets these results.

Note: I need to have Random() starting at 66, not adjusted to start at 72.

```For \$i = 1 To 4
Do
\$r2 = Random(66, 150, 1)
Until Mod(\$r2, 24) = 0
\$x = \$r2
MsgBox(0, '', \$x, 1);I want \$x to be either 66, 90, 114, or 138
Next                   ;but with this it's just giving either 72, 96, 120, or 144```

##### Share on other sites

```For \$i = 1 To 4
\$x = Random(1, 4, 1) * 24 + 42
MsgBox(0, '', \$x, 1);I want \$x to be either 66, 90, 114, or 138
Next```

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

Use an array.

\$a[4] = [66, 90, 114, 138]

MsgBox(0,"",\$a[Random(0,3,1)])

##### Share on other sites

```For \$i = 1 To 4
Do
\$r2 = Random(65, 139, 1)
Until \$r2 = 66 Or \$r2 = 90 Or \$r2 = 114 Or \$r2 = 138
MsgBox(0, '', \$r2, 1)
Next```

Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

##### Share on other sites

Am I missing something? Those are not multiples of 24.

##### Share on other sites

another one

```Do

\$x = Random(0, 1000, 1)
\$y = \$x/24
Until Not StringInStr(\$y,".")

MsgBox(0,"",\$x+66)```
Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)

There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

##### Share on other sites

Hello,

I want \$x to be any multiple of 24 starting at 66, ie either 66, 90, 114, or 138

I can use the Mod function to get multiples of 24, but how can i get multiples of 24 that

are starting from 66?

Thanks for any ideas and/or for code example that gets these results.

Note: I need to have Random() starting at 66, not adjusted to start at 72.

```For \$i = 1 To 4
Do
\$r2 = Random(66, 150, 1)
Until Mod(\$r2, 24) = 0
\$x = \$r2
MsgBox(0, '', \$x, 1);I want \$x to be either 66, 90, 114, or 138
Next                  ;but with this it's just giving either 72, 96, 120, or 144```
But as big_daddy pointed out, 66, 90, 114, and 138 are not multiples of 24

##### Share on other sites

But as big_daddy pointed out, 66, 90, 114, and 138 are not multiples of 24

If you start at 66 and add 24 to each you get the next in the series. I think that's all he's really going for. Regardless, weaponx solved his issue elegantly.

Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

##### Share on other sites

If you start at 66 and add 24 to each you get the next in the series. I think that's all he's really going for. Regardless, weaponx solved his issue elegantly.

ok I got it now

##### Share on other sites

I thank you all very much for your various solutions.

Sorry I couldn't get back right away to reply to your quick solutions.

It turns out that your solution Jos is the easiest one for me to use, and gets the results I want with code that is easiest for me to understand, being closely related to the code I posted.

About the resulting numbers being not multiples of 24:

Actually I'm not sure how to state it exactly, but, within the range of 66 to 150, I want \$x to be any number that is either 66, or some increments that are multiples of 24 (the size of the increments being multiples of 24, but not the numbers themselves being multiples of 24) is perhaps the more precise way to say it.

I should have probably mentioned that I want \$x to be MouseClick x position, and I need the MouseClick to happen within the range of 66 to 150, yet any MouseClick that happens within that range needs to be either 66, Or (66 + 24) Or (66 + 24 + 24), etc.

I want to keep it as simple as possible by just using the Random() and a loop, rather than having to type out multiple or ORs, and rather than making an array. So the code of Jos works best for me at this time it appears.

I was also hoping to see if anybody had a way to integrate Mod() into this code something like this:

Generate a random number from 66 to 150

If the generated number is some increment of 24 up from 66, then give that value to \$x.

Any way to do that with the Modulus function?

Thanks,

frew

##### Share on other sites

maybe this?

```Do
Sleep(10)
\$a = Random(66, 150, 1)
If Mod(\$a - 66, 24) = 0 Then
\$x = \$a
ExitLoop
EndIf

Until 0
MsgBox(0, '', \$x)```

##### Share on other sites

Very nice oMBRa!

Yes this works.

I'm not sure how it works yet though...still looking at the \$a - 66 and wondering how you got that?

I knew there must be a way with Mod(), but I couldn't see it.

Thanks so much.

frew

##### Share on other sites

you said the number u need is 66 + multiples of 24, so we can tell it as 66 + 24*x.

e.g 138 = 66 + 24*3 then I did 138 - 66 = 24*3, so if the remainder of 138 - 66 modulus 24 is 0 then is the number we were searching

I hope u get it, english is not my native language

##### Share on other sites

you said the number u need is 66 + multiples of 24, so we can tell it as 66 + 24*x.

e.g 138 = 66 + 24*3 then I did 138 - 66 = 24*3, so if the remainder of 138 - 66 modulus 24 is 0 then is the number we were searching

I hope u get it, english is not my native language

Math is not my native language.

Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

##### Share on other sites

I'm surprised this thread has continued so long, your first 2 replies were both efficient, excellent examples.

They generate a "hit" for each call to Random(), the solutions using Mod() mean you'll be calling Random() an average of 24 times for each number returned.

##### Share on other sites

Thanks for the explanation oMBRa.

I need to spend some time with this in order to really get it.

Somehow I cannot quite see how it works yet, but I'll come back to it after a little break.

Thanks again,

frew

##### Share on other sites

Okay, I think I get it now.

The Modulus function allows us to get the true multiples of a number.

The \$a - 66, for example, within the Modulus function "offsets" the "multiples", so they are no longer actually multiples, but now can be thought of a set of increments from a certain number.

I don't know if that explains it well, but I think I get it now.

Thanks so much for your help with this.

That helps so much with a project I'm working on.

frew

##### Share on other sites

the solutions using Mod() mean you'll be calling Random() an average of 24 times for each number returned

Thanks Spiff59, I did not know that.

I was just so curious to see if the Mod() could be part of the solution I was looking for. Something about that Mod() I like.

Well, thanks to all who helped me with this.

frew

## Create an account

Register a new account

×

• Wiki

• Back

• Git