Jump to content

Not sure how Random works?


Recommended Posts

Hi, I want my script to run something different 9 times out of 10 times, but on the rare occasion that it lands on 1 I want it to sleep. What I've wrote isn't working, it always shows "Failed: Message not sent", it never shows "Success: Message sent"

Basically what I'm after is for it to get a number between 1 and 10, and if that number is 1, then sleep, if its anything other than 1 do something else.

#include <Misc.au3>

HotKeySet("{F1}","stopScript")

While 1
sleep(Random(1000, 3000))
   If Random(1, 10, 1) Then
      sleep(Random(1000, 3000))
      ConsoleWrite("Failed: Message not sent" & @CRLF)
   Else
      ConsoleWrite("Success: Message sent" & @CRLF)
   EndIf
WEnd

Func stopScript ()
   msgbox (0, "Stopping script", "Script has been stopped.")
   Exit
EndFunc

 

Link to comment
Share on other sites

36 minutes ago, Dxnny said:

Basically what I'm after is for it to get a number between 1 and 10, and if that number is 1, then sleep, if its anything other than 1 do something else

How is this topic different from the one below?  If it isn't, then why did you create a new topic?

 

Edited by TheXman
Link to comment
Share on other sites

Link to comment
Share on other sites

8 minutes ago, Nine said:

This will always return true since you ask for a random number between 1 and 10.  Only the number 0 is consider False.

How do I make it all the other numbers except for 1 then?

Link to comment
Share on other sites

Just for fun, another variation ;):

#include <Misc.au3>

HotKeySet("{F1}", "stopScript")
Local $bSuccess, $iRandom
While 1
    Sleep(Random(1000, 3000))
    $iRandom  = Random(1, 10, 1)
    $bSuccess = ($iRandom = 1) ? True : False

    If Not $bSuccess Then
        Sleep(Random(1000, 3000))
        ConsoleWrite("!Random Value = " & StringFormat("%2i", $iRandom) & " ==> Failed: Message not sent" & @CRLF)
    Else
        ConsoleWrite("+Random Value = " & StringFormat("%2i", $iRandom) & " ==> Success: Message sent" & @CRLF)
    EndIf
WEnd

Func stopScript()
    MsgBox(0, "Stopping script", "Script has been stopped.")
    Exit
EndFunc   ;==>stopScript

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...