TheFixer

Autoit Random number help?

9 posts in this topic

So I need some help...

basically all I want to do is have Autoit generate a random number between 1 and 3

and then have an if statement that reads the random number... and lets say if it = 3 it will say the number 3 in a msgbox

(like this)

sleep(500)

Random(1,3)

If Random = 2 Then
    MsgBox(0,"","2")
EndIf

If Random = 1 Then
    MsgBox(0,"","1")
EndIf

If Random = 3 Then
    MsgBox(0,"","3")
EndIf

can anyone help me? (thanks in advance!)

Share this post


Link to post
Share on other sites



$RandomNumber=Random(1,3,1)
MsgBox(0,"Result",$RandomNumber)

 

not exactly what i was looking for I want it to be like
 

If $RandomNumber = 3 Then
--> does stuff here
EndIf

 

Share this post


Link to post
Share on other sites

TheFixer,

Get the random number and then use a Switch structure to distinguish between the 3 returns - look in the Help file to see the syntax.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

TheFixer,

Get the random number and then use a Switch structure to distinguish between the 3 returns - look in the Help file to see the syntax.

M23

I hope you don't mind but could you give me a little example? (I work best with examples & having things explained to me like I'm stupid pretty much lol)

Share this post


Link to post
Share on other sites

TheFixer,

What do you not understand about the example for Switch in the Help file? Does that not explain how to use the function well enough?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

$iRandomNumber = Random(1, 3, 1)

Switch $iRandomNumber
    Case 1
        ; Code
    
    Case 2
        ; Code
    
    Case 3
        ; Code
EndSwitch

 

Edited by TheDcoder
speeling
1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
$iRandomNumer = Random(1, 3, 1)

Switch $iRandomNumer
    Case 1
        ; Code
    
    Case 2
        ; Code
    
    Case 3
        ; Code
EndSwitch

 

Thank you very much :)

Share this post


Link to post
Share on other sites

My pleasure :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

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

  • Similar Content

    • MarkBe
      By MarkBe
      Hi
      I am trying to check a number of applications are up to date and install the update if required. This will run as a shutdown script, pushed out through Group Policy.
      I am querying various registry keys to determine if
      The software is actually installed The software is at the correct version Once the need for update is determined then it calls another function to run the update.
      If the software is up to date or not installed, it moves down to the next software section which changes the registry key variables and calls the registry query function again
      I have managed to get the function for (un)installs to work but I am having trouble with the registry query function
      #NoTrayIcon #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> ; Here we go! ;########################################################################################################## ;Declare variables Global $RegKey86, $RegKey64, $RegValueName, $RegValueData, $CheckVersion86, $CheckVersion64 ; 7Zip ;########################################################################################################## _7Zip_Function() Func _7Zip_Function() ; Registry location and values that are checked for current software version ; Ensure the x64 key is preceeded with 'HKLM64' $RegKey86="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip" $RegKey64="HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip" $RegValueName="DisplayVersion" $RegValueData="16.04" ; Excecution ; --------------------------------------------------------------------------------------------------------- If _CheckSW() = 1 Then _UninstallOldVer() _InstallSW() EndIf EndFunc ;==>_7Zip_Function ; Functions ;########################################################################################################## Func _CheckSW() $CheckVersion86=RegRead ($RegKey86, $RegValueName) $CheckVersion64=RegRead ($RegKey64, $RegValueName) If $CheckVersion86=$RegValueData Or $CheckVersion64=$RegValueData Then Return 0 ElseIf $CheckVersion86="" And $CheckVersion64="" Then Return 0 Else Return 1 EndIf EndFunc ;==>CheckSW  
    • hcI
      By hcI
      Hello everyone !
      I've made a little function who return random characters for everyone who would need it.. It's my first function so please have some mercy.
      And if you have some suggestions.. don't hesitate !
      and I made a AutoIt HelpFile that looks like others helpfiles, I hope you'll enjoy !
      The function :  RandomLetter.au3
      The helpfile : RandomLetter.html  (in HTML please !)
      Et pour les anglophobes, voilà l'aide en français : RandomLetter.fr.html (French version of the helpfile)
      Thanks, hcI
    • 31290
      By 31290
      Hi guys, 
      Hope you are fine today
      I'm trying to restart a function by calling it back in the script:
      Here's my code so far:
      Func f_VPN() $iInputBox = InputBox("Password", "Please Enter the User's password.") If @Error = 1 Then GUICtrlSetState($fVPN, $GUI_UNCHECKED) Else ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe") $hVPN = WinWait("Cisco AnyConnect Secure Mobility Client", "Ready to connect") ControlClick($hVPN, "", "Button1") $hVPN1 = WinWait("Cisco AnyConnect | SEE VPN", "Cancel") ControlSetText($hVPN1, "", "Edit2", @Username) Sleep(250) ControlSetText($hVPN1, "", "Edit3", $iInputBox) Sleep(250) ControlClick($hVPN1, "", "Button1") Sleep(5000) If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Login Failed" Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button4") WinClose($hVPN) f_VPN() Else ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe") If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Connected To SEE VPN." Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button1") Sleep(1000) WinClose("Cisco AnyConnect Secure Mobility Client", "") ProcessClose("vpnui.exe") ; ProcessClose("explorer.exe") <<< TO UNCOMMENT IniWrite($oIniFile, "LaptopChkBox", "VPN", "1") EndIf EndIf EndFunc I'd like to the function to be restarted in the case the password provided is not correct. 
      I've googled many things but nothing very relevant.
      Any ideas over here?
      Thanks
      -31290
       
    • WoodGrain
      By WoodGrain
      Hi guys,
      Could someone please tell me what I'm doing wrong with this code?
      I'm trying to take user input and then run a function with the same name, but it's not calling the function.
      So in the below after pressing Shift+F8 I would enter the text (without quotes) "testFunc".
      As you can see I've also tried this with IF statements, I'm new to using Switch/Case.
      HotKeySet("+{F8}", RunManually) Func RunManually() ;Use a case statement with 1 hot key and an InputBox to manually run functions $funcName = InputBox("Which Func to Run?", "Enter the name of the function to run") MsgBox(0, "Entered value", $funcName) ;If $funcName = testFunc Then testFunc() ;If $funcName = test2Func Then test2Func() Switch $funcName Case testFunc MsgBox(0, "Calling", "Calling Function") testFunc() MsgBox(0, "Called", "Function call finished") Case test2Func MsgBox(0, "Calling", "Calling Function") test2Func() MsgBox(0, "Called", "Function call finished") EndSwitch EndFunc While 1 ;testFunc() Sleep(1000) WEnd Func testFunc() MsgBox(0, "func running", "Seems to work!") EndFunc Func test2Func() MsgBox(0, "func 2 running", "2 Seems to work!") EndFunc Thanks!
    • mazz3d
      By mazz3d
      hello  everyone,
      I'm new with AutoIT and just looking for a way to open Firefox with random window size
      i have tried too many different samples from the web, some how it works but i can't make it works with Firefox 
      MozRpel and ff.au3 installed 
      right now i can open Firefox with this code, but i still can't control the window of Firefox
      ; open firefox #include <ff.au3> _FFStart() ; open firefox with random size