Sign in to follow this  
Followers 0
Dana

Random(): differences script vs compled exe

15 posts in this topic

#1 ·  Posted (edited)

I'm finding some more weirdness in the Random function.

Do
    (some stuff)
    $randidx[$i] = Random(1, $array[0], 1) ; pick a random integer between 1 and $array[0] 
    (some more stuff)
    $i = $i + 1
Until $i = $desired + 1

If I run the script from SciTE, a get a different set of random numbers each time, as expected. However, if the script is compiled to an exe, I get the same set of numbers each time I run the exe.

Is this a bug? What is the Random function using as a seed if SRandom isn't used?

It doesn't seem to be too big a deal; adding:

SRandom(_Date_Time_GetTickCount())

just before the Do loop solves it even in the exe, but I'd like to know what's going on... and why the compiled executable should behave differently from the uncompiled script?

Edited by Dana

Share this post


Link to post
Share on other sites



Dana,

Just tried and I get random random numbers each time I run this script both in SciTE and compiled: :)

Global $aArray[10]

For $i = 0 To 9
    $aArray[$i] = Random(0, 1000, 1)
Next

MsgBox(0, "Result", $aArray[0] & @CRLF & _
                    $aArray[1] & @CRLF & _
                    $aArray[2] & @CRLF & _
                    $aArray[3] & @CRLF & _
                    $aArray[4] & @CRLF & _
                    $aArray[5] & @CRLF & _
                    $aArray[6] & @CRLF & _
                    $aArray[7] & @CRLF & _
                    $aArray[8] & @CRLF & _
                    $aArray[9])

I also use the Random function in my desktop MP3 player and it certainly does not repeat the same tracks each time I run it. ;)

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

I dunno why it should be different, but I consistent got the same set of "random" numbers each time I ran the exe version... that's why I was wondering what gets used as a seed if you don't use the SRandom function first.

Hmmm... if I compile it (your example) with obfuscator, it breaks and I get the same series every time. If I don't use obfuscation, it works as it should.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Your post is empty Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Dana,

Even when the script above is Obfuscated (/sf /sv /om /cs=0 /cn=0) I still get random random numbers. :)

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

$randidx[$i] = Random(1, $array[0], 1) ; pick a random integer between 1 and $array[0]

Is $array[0] returning a value of 1 by any chance? If the Min and Max are the same number, Random returns a 0 and sets @error.

See this discussion -->

If that is your case, I posted a little function to "fix" it in that thread.

Share this post


Link to post
Share on other sites

No, $array[0] is in the neighborhood of 5,000 or so.

Melba23, I'm using the default Obfuscator settings during compile.

Strange... at work I get the repeated results I described with the above example. At home, it works properly. Guess I'll have to wait until Monday to see what the difference may be.

Interestingly, I looked at the obfuscated au3 file and I see that obfuscator added the line:

SRandom(Number(StringRight(TimerInit(),4)))
even though SRandom isn't used in the original code.

Share this post


Link to post
Share on other sites

Grrr... returning to the office this morning, both the obfuscated and unobfuscated compiled versions return a different random series each time, as it should.

Which makes me question: The obfuscated version adds the line with the SRandom function which isn't in my original script. (why?) That line uses TimerInit as input to SRandom. I know TimerInit "Returns a timestamp (in milliseconds)" according to the helpfile, but a time based on what? If the computer has been on for a long time (a week or two, as it was before I shut it down last Friday) would it "max out" and return the same value each time?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Ofcourse, if you have a truly random dice and you throw it an infinite number of times, the probably of it rolling on the same number a hundred times in a row are approaching 100%. Maybe you were the lucky one. :)

In all the years I have used AutoIt I have never seen the timestamp "max out". That would also probably have been noticed, since it will lead to unexplained bugs in long-running scripts in a production environment and cause someone to do some serious debugging.

Perhaps you were facing an issue like this one:

While 1
    $time = TimerInit()
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    $time = 5
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    ; Do all kinds of things
    
    ; Oh how convenient, don't need to call timerinit again
    SRandom($time)
    ; Pure delicous rando~.. OH WWHAT!! *Makes thread*
    ConsoleWrite(Random(0, 10) & @CRLF)
WEnd
Edited by Manadar

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Your post is empty Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Your post is empty Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Your post is empty Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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  
Followers 0