Jump to content
Sign in to follow this  
Qwerty212

Import Random function to Autoit

Recommended Posts

Qwerty212

Hello.

Time ago, looking examples in the forum, I wrote a little script that looks for the mp3 and wav files in @scriptsfolder and plays one of them randomly:

#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
#include <ButtonConstants.au3>
#include <Sound.au3>
#include <File.au3>
GUICreate("PRESS ESCAPE TO CLOSE SOUND PLAYING", 560, 380, -1, -1)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("PRESS ESCAPE TO CLOSE SOUND PLAYING", 30, 40, 500, 300, $SS_CENTER)
GUICtrlSetFont(-1, 50, 10, 0)AutoItSetOption ( "GUIOnEventMode", 1)
GUISetOnEvent ( $GUI_EVENT_CLOSE, "On_Exit" )
Func On_Exit()
Exit
EndFunc ;==>On_ExitHotKeySet("{ESC}", "On_Exit")
$folder = @ScriptDir$list = _FileListToArray($folder, "*.mp3|*.wav", 1)

For $iX = 1 To $list[0]
$song = _SoundOpen($folder & "\" & $list[$iX])
_SoundPlay($song)
Do
Sleep(100)
Until _SoundStatus($song) = "stopped"
_SoundClose($song)
canciones()
Next
While 1
$nmsg = GUIGetMsg()
Select
Case $nmsg = $GUI_EVENT_CLOSE
On_Exit()
EndSelect
WEnd

Func canciones()
For $iX = 1 To $list[0]
$song = _SoundOpen($folder & "\" & $list[$iX])
_SoundPlay($song)
Do
Sleep(100)
Until _SoundStatus($song) = "stopped"
_SoundClose($song)
canciones()
NextEndFunc ;==>canciones

Later talking with a friends he said me that:

I found out that my random numbers I generate with my program are not THAT random, ... it's hard to explain, since it's all random there are no real facts, but only a feeling after lot's of testing.

And the feeling says that it's not totally random as in "the law of random", ... it's just had too many of the same random numbers.

Then I googled and found a solution which works now fine for me, ... when I create a random number it looks like this in C# code:

The fix which makes the randomness even more random is this:

Random randomNumber = new Random(Guid.NewGuid().GetHashCode());

int newRandomNumber = randomNumber.Next(1, 101);

I have exchanged every "new Random();" with that and now it "feels" much more random. He asked me if I was able to port his code to my little Autoit script to generate random number, but my random number comes from the number of sound files found in the script's folder and his code comes from Guid.newGuid() and from GetHashCode() and always based on a number from 1 to 100.

Is there any way to implement his code into mine's?

Thanks in advance.

Greets from Barcelona

(Now I have to go to work, so I will edit the post later trying to be more polite)

Share this post


Link to post
Share on other sites
guinness

The code seems a little badly formatted. But to answer your question you can use the return value from _FileListToArray.

#include <Constants.au3>

Local $aArray = _FileListToArray(...
Local $iIndex = Random(1, $aArray[0], 1)
MsgBox($MB_SYSTEMMODAL, '', $aArray[$iIndex])
Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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  

×