DesireDenied Posted February 1, 2021 Share Posted February 1, 2021 Anyone want to help me randomize this algorithm? Right now, despite a series of randomizations, all numbers are drawn almost the same. The difference is slight. Func GenerateNumbers() Local $aResult[0] For $i = 1 To 12 $r = Random(1, 49, 1) $r2 = Random(1, 4) Switch($r2) Case 1 If $r>=1 And $r<=15 Then $r = Abs(Random(-$r, 25, 1)) Case 2 If $r>=16 And $r<=31 Then $r = Abs(Random(-$r, 15, 1)) Case 3 If $r>=32 And $r<=49 Then $r = Abs(Random(-$r, 0, 1)) case 4 EndSwitch _ArrayAdd($aResult, $r) Next $aResult = _ArrayUnique($aResult, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT) $aResult = _ArrayExtract($aResult, 0, 5) Return $aResult EndFunc Thanks in advance. Link to comment Share on other sites More sharing options...
Danp2 Posted February 1, 2021 Share Posted February 1, 2021 Have you tried calling SRandom? DesireDenied 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 (edited) Actually, I've never used it, this is my very first time, but it does not change my results too much. Func GenerateNumbers() Local $aResult[49] Local $iTotalNumbers = 0 Local $r = 1 Local $iSeed = 1 Do $iSeed = Random(1, 14000000, 1) SRandom($iSeed) $r = Int(Random()*100) If $r >= 1 And $r <= 49 Then ConsoleWrite($r & @CRLF) $aResult[$iTotalNumbers] = $r $iTotalNumbers += 1 EndIf Until $iTotalNumbers = 12 $aResult = _ArrayUnique($aResult, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT) $aResult = _ArrayExtract($aResult, 0, 5) Return $aResult EndFunc Edited February 1, 2021 by DesireDenied Link to comment Share on other sites More sharing options...
Nine Posted February 1, 2021 Share Posted February 1, 2021 Help file says : Quote After each call to SRandom() random number generator starts a new sequence. Normally you should call it once for the whole sequence, not before each call to random DesireDenied 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 Thanks. Nothing really much has changed. Func GenerateNumbers() Local $aResult[12] Local $iTotalNumbers = 0 Local $r = 1 Local $iSeed = Random(1, 28000000, 1) SRandom($iSeed) Do $r = String(Random()) $r = StringMid($r, Random(2, 12, 1), 2) $r = Int($r) If $r >= 1 And $r <= 49 Then ConsoleWrite($r & @CRLF) $aResult[$iTotalNumbers] = $r $iTotalNumbers += 1 EndIf Until $iTotalNumbers = 12 $aResult = _ArrayUnique($aResult, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT) $aResult = _ArrayExtract($aResult, 0, 5) Return $aResult EndFunc I have also started to experiment with Sin() but it does make bigger difference. Func GenerateNumbers() Local $aResult[12] Local $iTotalNumbers = 0 Local $r = 1 Do $r = Int(Sin(Random(1, 14000000))*100) If $r >= 1 And $r <= 49 Then ConsoleWrite($r & @CRLF) $aResult[$iTotalNumbers] = $r $iTotalNumbers += 1 EndIf Until $iTotalNumbers = 12 $aResult = _ArrayUnique($aResult, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT) $aResult = _ArrayExtract($aResult, 0, 5) Return $aResult EndFunc Do you have any other ideas how to make draws more chaotic? Link to comment Share on other sites More sharing options...
RTFC Posted February 1, 2021 Share Posted February 1, 2021 3 minutes ago, DesireDenied said: Local $iSeed = Random(1, 28000000, 1) SRandom($iSeed) This still produces the same sequence every time you run it. You need to use an external seed when calling SRandom, for example @AutoitPID or @MSEC (or a combination thereof) BEFORE ever calling Random. DesireDenied and Danp2 1 1 My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
Nine Posted February 1, 2021 Share Posted February 1, 2021 What is it you want to achieve ? Random numbers from what to what ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 Oh yeah! It works! I made to separate functions with totally different seeds, and now the difference is huge. expandcollapse popupFunc GenerateCoupon() Local $aResult[12] Local $iTotalNumbers = 0 Local $r = 1 Local $iSeed = @MSEC + TimerInit() SRandom($iSeed) Do $r = String(Random()) $r = StringMid($r, Random(2, 12, 1), 2) $r = Int($r) If $r >= 1 And $r <= 49 Then ConsoleWrite($r & @CRLF) $aResult[$iTotalNumbers] = $r $iTotalNumbers += 1 EndIf Until $iTotalNumbers = 12 $aResult = _ArrayUnique($aResult, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT) $aResult = _ArrayExtract($aResult, 0, 5) Return $aResult EndFunc Func GenerateDraw() Local $aResult[12] Local $iTotalNumbers = 0 Local $r = 1 Local $iSeed = @AutoItPID + @MSEC SRandom($iSeed) Do $r = String(Random()) $r = StringMid($r, Random(2, 12, 1), 2) $r = Int($r) If $r >= 1 And $r <= 49 Then ConsoleWrite($r & @CRLF) $aResult[$iTotalNumbers] = $r $iTotalNumbers += 1 EndIf Until $iTotalNumbers = 12 $aResult = _ArrayUnique($aResult, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT) $aResult = _ArrayExtract($aResult, 0, 5) Return $aResult EndFunc Link to comment Share on other sites More sharing options...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 @Nine I want to have totally chaotic distribution of numbers between 1-49. Example from above satisfies me a lot, but i will now try to add some more random variables to seeds. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 1, 2021 Moderators Share Posted February 1, 2021 (edited) DesireDenied, If that is all you need then I suggest simply using _ArrayShuffle: #include <Array.au3> Global $aArray[50] For $i = 1 To 49 $aArray[$i] = $i Next _ArrayShuffle($aArray, 1, 49) _ArrayDisplay($aArray, "", Default, 8) The function uses the Fisher-Yates shuffle algorithm, so should be properly random. M23 Edited February 1, 2021 by Melba23 Added link 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...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 @Melba23 Thanks I am trying to make occurrences of all numbers more chaotic. Right now some of the occurrences are too uniform. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 1, 2021 Moderators Share Posted February 1, 2021 DesireDenied, You appear to be confusing "truly random" with what you perceive as "random". It is quite likely that successive numbers in a truly random sequence will be close neighbours - just look at any lottery draw. So trying to avoid such occurrences is in fact making the series less random, not more. M23 Sidley, Earthshine and DesireDenied 1 2 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...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 @Melba23 Hmm, actually, you might be right... I have tried to use shuffle, but it gives me very uniform pattern. Link to comment Share on other sites More sharing options...
Nine Posted February 1, 2021 Share Posted February 1, 2021 Try this : #include <Constants.au3> #include <Array.au3> Const $MAX = 49 Local $aArray[$MAX] For $i = 1 to 500 GenerateNumber(20) Next _ArrayDisplay($aArray) Func GenerateNumber($iNum) SRandom(Int(@MSEC)) For $i = 1 to $iNum $aArray[Random(1,$MAX,1)-1] += 1 Next Sleep(Random(5,15,1)) EndFunc Looks to me quite random... DesireDenied 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 @Melba23 You're completely right. Idea to make draws more chaotic would only damage final results of my simulations. Thanks a lot. Also thanks for your code, its not is twice faster than generating array of 5 element and using random, it is almost that good as statistics from the game I am trying to simulate. Link to comment Share on other sites More sharing options...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 6 minutes ago, Nine said: Try this : #include <Constants.au3> #include <Array.au3> Const $MAX = 49 Local $aArray[$MAX] For $i = 1 to 500 GenerateNumber(20) Next _ArrayDisplay($aArray) Func GenerateNumber($iNum) SRandom(Int(@MSEC)) For $i = 1 to $iNum $aArray[Random(1,$MAX,1)-1] += 1 Next Sleep(Random(5,15,1)) EndFunc Looks to me quite random... @Nine Thanks, it works, but its very slow. But it is a good lesson to me Link to comment Share on other sites More sharing options...
Nine Posted February 1, 2021 Share Posted February 1, 2021 Yes it is kind of slow, did not know it was a criteria “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
DesireDenied Posted February 1, 2021 Author Share Posted February 1, 2021 Forgot to mention I am running millions of simulations with different number of coupons / week, different jackpots, and checking results. So far @Melba23 solution is closest to original game statistics and is fast as hell. Link to comment Share on other sites More sharing options...
Werty Posted February 1, 2021 Share Posted February 1, 2021 Semi-ontopic,... I needed a "FairRandom" function once and used nested randoms... Random(0, Random(0, Random(0, Random(0, Random(0, Random(0, Random(0, Random(0, 99999999), 1), 1), 1), 1), 1), 1), 1 This way a fair amount of each "Block" of numbers gets picked, like, an equal amount of numbers between 0 and 10000, and 10000 to 20000, 20000 to 30000 and so on. Like a lottery with numbers from 1 to 1 million, most numbers picked would be way above 100000, with very few numbers between 1 100000 being picked, with nested randoms the drawing is more fair. This is probably very slow though, but works fine and fair if it was a lottery drawing. Some guy's script + some other guy's script = my script! Link to comment Share on other sites More sharing options...
Nine Posted February 1, 2021 Share Posted February 1, 2021 Here my attempt with fast performance criteria in : #include <Constants.au3> #include <Array.au3> Const $MAX = 49 Local $aArray[$MAX] For $i = 1 to 500 GenerateNumber(20) Next _ArrayDisplay($aArray) Func GenerateNumber($iNum) SRandom(Mod(DllCall ("kernel32.dll", "bool", "QueryPerformanceCounter", "uint64*", 0)[1],111)) For $i = 1 to $iNum $aArray[Random(1,$MAX,1)-1] += 1 Next EndFunc “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy 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