Jump to content
Sign in to follow this  
frew

How to do code If Random(1, 50, 1) = 5,10,15,20,25,30,35,40,45, OR 50?

Recommended Posts

frew

Hello,

I'm trying to find the best way to generate a random number between, for example, 1 and 50, but have the code execute only when one of the randomly generated numbers is one of a chosen subset of numbers.

I can't quite work out hos to do it without having to do a big batch of If Else.

I was hoping to find a way to arrange the syntax something like this:

$i = Random(1, 50, 1)

If $i = (5,10,15,20,25,30,35,40,45, OR 50) Then

;do stuff

EndIf

Thanks for any ideas.

frew

Share this post


Link to post
Share on other sites
jchd

$i = Random(1, 50, 1)
If Mod($i, 5) = 0 Then
   ;do stuff
EndIf

JChD


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
frew

Okay, Thank you jchd, I see what you mean, when I use those kinds of numbers that are divisible like that, but what if I had numbers like:

$i = Random(1, 373, 1)

If $i = (1, 17, 67, 84, 111, 152, 201, 297, 330, OR 341) Then

;do stuff

EndIf

Thanks for any other ideas.

frew

Share this post


Link to post
Share on other sites
andybiochem

try loading values into an array, and selecting index at random:

Global $array[10] = [1, 17, 67, 84, 111, 152, 201, 297, 330, 341]

$random = $array[Random(0,UBound($array) - 1,1)]

MsgBox(0,"",$random)

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites
zorphnog

One more suggestion:

$i = Random(1, 50, 1)
Switch $i
    Case 5,10,15,20,25,30,35,40,45,50
        ;do stuff
    Case Else
        ;do other stuff
EndSwitch

Share this post


Link to post
Share on other sites
jchd

Okay, Thank you jchd, I see what you mean, when I use those kinds of numbers that are divisible like that, but what if I had numbers like:

$i = Random(1, 373, 1)

If $i = (1, 17, 67, 84, 111, 152, 201, 297, 330, OR 341) Then

;do stuff

EndIf

Then, the simplest and fastest I can think of is:

$i = Random(1, 373, 1)
Switch $i
    Case 1, 17, 67, 84, 111, 152, 201, 297, 330, 341
      ;do stuff
    Case Else
      ; do else case (optional)
EndIf

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
frew

Thanks so much jchd and zorphnog for the Switch Case idea.

Yes, that's what I was hoping for. Very nice!

Thanks also andybiochem for the arrays idea. I'll check that too, but I'm not familiar with arrays yet.

So this kind of thing works:

$i = Random(1, 100, 1)
Switch $i

    Case 1 To 50
        $msg = "1 to 50"

    Case 51, 52
        $msg = "51,52"

    Case 53 To 100
        $msg = "53 to 100"
        
    Case Else
        $msg = "Case Else"

EndSwitch

MsgBox(0, Default, $msg)

Thanks again!

frew

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×