Radiance Posted December 3, 2014 Share Posted December 3, 2014 Hi all, sometimes when I'm generating two random numbers just after another using the Random() function, I'm getting the same values. This happens far too often to be random. Has anybody else experienced this? Is there any way to avoid it without adding a sleep between the two randoms? Link to comment Share on other sites More sharing options...
computergroove Posted December 3, 2014 Share Posted December 3, 2014 I havent experienced this but it is possible. Its maybe more likely that there is a coding error. You could make an if statement that says: Global $LastRandom While 1 Local $Random = Random(0,10,1) If $LastRandom == $Random Then Do $Random = Random(0,10,1) Until $LastRandom <> $Random MsgBox(0,"Random Number",$Random) Else MsgBox(0,"Random Number",$Random) EndIf $LastRandom = $Random WEnd Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html Link to comment Share on other sites More sharing options...
czardas Posted December 3, 2014 Share Posted December 3, 2014 (edited) If you try to avoid the same number repeating, then you are avoiding the random chance that the same number will repeat. If you run Random(1, 10, 1) 1000 times, each number (1-10) will occur on average about 100 times - in any order. Edited December 3, 2014 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
computergroove Posted December 3, 2014 Share Posted December 3, 2014 When I ran random I was getting the same number in a row around 1 in 10-15 tries. I didn't see the issue you are having. Can you provide some code? Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html Link to comment Share on other sites More sharing options...
Solution Yashied Posted December 3, 2014 Solution Share Posted December 3, 2014 Try to use SRandom() at the beginning of your script: SRandom(@AutoItPID) czardas and Radiance 2 My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
computergroove Posted December 3, 2014 Share Posted December 3, 2014 I made this with the help of a moderator to show the results of random: expandcollapse popup#include <GUIConstantsEx.au3> Global $aLabel[11], $bStart = False $hGUI = GUICreate("Form1", 157, 438, 336, 218) GUICtrlCreateLabel("Random Number Count", 16, 16, 115, 17) For $i = 1 To 10 GUICtrlCreateLabel("1", 24, 152 + (24 * $i), 10, 17) $aLabel[$i] = GUICtrlCreateLabel(0, 64, 152 + (24 * $i), 30, 17) Next GUICtrlCreateLabel("Speed", 32, 120, 35, 17) $cSpeed = GUICtrlCreateInput(100, 24, 136, 49, 21) $cStart = GUICtrlCreateButton("Start", 24, 48, 89, 25) $ButtonResetCount = GUICtrlCreateButton("Reset Count", 24, 80, 89, 25) GUISetState(@SW_SHOW) $nTimeStamp = TimerInit() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cStart Switch GUICtrlRead($cStart) Case "Start" GUICtrlSetData($cStart, "Stop") $bStart = True Case Else GUICtrlSetData($cStart, "Start") $bStart = False EndSwitch Case $ButtonResetCount ResetCount() EndSwitch If $bStart Then If TimerDiff($nTimeStamp) > GUICtrlRead($cSpeed) Then $iRandom = Random(1, 10, 1) GUICtrlSetData($aLabel[$iRandom], GUICtrlRead($aLabel[$iRandom]) + 1) $nTimeStamp = TimerInit() EndIf EndIf WEnd Func ResetCount() For $i = 1 To 10 GUICtrlSetData($aLabel[$i], 0) Next $bStart = False EndFunc ;==>ResetCount Radiance 1 Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html Link to comment Share on other sites More sharing options...
Radiance Posted December 3, 2014 Author Share Posted December 3, 2014 (edited) If you try to avoid the same number repeating, then you are avoiding the random chance that the same number will repeat. If you run Random(1, 10, 1) 1000 times, each number (1-10) will occur on average about 100 times - in any order. I am aware of this. Since the numbers I use are usually too big and I get this result pretty often it's hard to believe that it's still random. Sorry. I forgot to add that I'm only having this issue when running more than one script on random. Check this out: This is just an example I did a minute ago. I've got a server software launching other AutoIt scripts, using asynchronous communication between them. They get identified by a longer number I'm rolling at random when the "child"script starts. When two of them happen to get launched directly after anouther they get the same ID and I get really weird results. Edit: I made this with the help of a moderator to show the results of random This is really awesome to look at. Edited December 3, 2014 by Radiance czardas 1 Link to comment Share on other sites More sharing options...
Exit Posted December 3, 2014 Share Posted December 3, 2014 I made this with the help of a moderator to show the results of random: YOU made nothing !!! It is the original code of MELBA23 And you even copied the error in line 8: GUICtrlCreateLabel(1, 24, 152 + (24 * $i), 10, 17) should be GUICtrlCreateLabel($i, 24, 152 + (24 * $i), 10, 17) App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
computergroove Posted December 3, 2014 Share Posted December 3, 2014 (edited) Wow.... I made the idea, I made the original faulty code, I pursued the right answer. I ran it and it worked. I made it to share in this post and I even gave props to the moderator for helping. What's the big deal here? Rest assured Im not getting paid for my efforts nor am I hiding anything. When you are running 2 of these back to back you are probably doing so withing the same second (This is a guess). Try adding sleep(1000) between those and see if the results are different. From what I have heard there is no real random in computer language. The program is probably taking the date and time and running it through an algorithm and displaying the results. If you change a variable it should change the output. Edited December 3, 2014 by computergroove Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html Link to comment Share on other sites More sharing options...
MikahS Posted December 3, 2014 Share Posted December 3, 2014 Wow.... I made the idea, I made the original faulty code, I pursued the right answer. I ran it and it worked. I made it to share in this post and I even gave props to the moderator for helping. What's the big deal here? Rest assured Im not getting paid for my efforts nor am I hiding anything. When you are running 2 of these back to back you are probably doing so withing the same second (This is a guess). Try adding sleep(1000) between those and see if the results are different. From what I have heard there is no real random in computer language. The program is probably taking the date and time and running it through an algorithm and displaying the results. If you change a variable it should change the output. A link to the topic would have been just fine. Back on topic, is the goal to run more than one script using random at the same time? Snips & Scripts My Snips: graphCPUTemp ~ getENVvarsMy Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4 Feel free to use any of my code for your own use. Forum FAQ Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted December 3, 2014 Moderators Share Posted December 3, 2014 @Exit and @computergrove, play nice. @Radiance, Yashield gave you what you needed in post #5, you need a seed basically. <snip>, cute, I wrote out basically what SRandom said in the help file before I thought that it was probably explained there!.... Just see the helpfile lol czardas 1 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
czardas Posted December 3, 2014 Share Posted December 3, 2014 I forgot to add that I'm only having this issue when running more than one script on random. Interesting discovery. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 3, 2014 Moderators Share Posted December 3, 2014 Hi,I knew this had been discussed before, but it took a while to find it. M23 Radiance 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Unc3nZureD Posted December 4, 2014 Share Posted December 4, 2014 As it had been said, use SRandom, so you can set a seed. Probably Autoit automatically uses time() function as a seed, which can cause such problems. If you choose a better Seed (like the previously mentioned PID) you can avoid it. Radiance 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now