Sign in to follow this  
Followers 0
Dana

Random number weirdness

4 posts in this topic

I'm seeing strange things with the Random function.

The first time I use it (in a loop to select some numbers, say 10), it works OK:

Random(1, $array[0], 1) where $array[0] is 4675:

3464, 1354, 3989, 3166, 2493, 1020, 238, 906, 3433, 2576

Then, the same syntax in another part of the program but run one at a time with a msgbox each time to check it, gives me clearly not random results:

14, 14, 5, 984, 7, 984, 978, 13, 6, 13, 10, 3, 10, 4, 6, 4, 7, 14, 2657, 2659, 2646, 7, 2, 3, 5, 6, 3790, 13, 3493, 841, 9, 11, 5, 6, 3496, 3, 3790, 13, 978, 12, 984....

As you can see there is a clear grouping of numbers in certain ranges. What's odd is that I don't see this grouping in the initial looped run. But during the second stage, the numbers are always in the same ranges (grouped around 1-10, 980, 3490, etc.)

I didn't use the SRandom function to set the seed... if you don't use it what does the Random function use? However, if I do use the SRandom function, setting the seed with a number calculated form a timer function (based on how long the user waits before clicking OK), I still get the same kinds of numbers.

What am I missing?

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

SRandom(), if you don't use it what does the Random function use?

Some random seed value is used at the start I'm correct. (If you like to know the used seed number, for reverence or debugging purpose, you have to use SRandom() with your own fixed or random generated seed number/value.

---

if I do use the SRandom function, setting the seed with a number calculated form a timer function (based on how long the user waits before clicking OK), I still get the same kinds of numbers.

Your probably feeding SRandom() a value out of its accepted range.

note:

- timer: 0 ... 2^63-1 (might have no direct limit, considering its returned as double/float type)

- SRandom(): -2^31 ... 2^31-1

---

Global Const $INT31u = Int('0x0080000000') ;; (2^31), int64. (2147483648)
Global Const $INT32u = Int('0x0100000000') ;; (2^32), int64. (4294967296)
Func ___RandomTimedSeed() ;; Set seed base on current timer value, returns used seed value.
    Local $iSeed = Mod(Int(TimerInit()), $INT32u) - ($INT31u) ;; wraps to [-2^31 and 2^31-1]. (+ value shift)
    SRandom($iSeed)
    Return $iSeed
EndFunc
(oops, better add the globals I use to.) Edited by MvGulik

"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

#4 ·  Posted (edited)

Ah, got it. Thanks for the suggestions anyway. Not a problem with the random function, but a typo on my part exposing a different problem.

My script is manipulating randomly selected files. Along the way it checks the file size. Seems AutoIt doesn't support unicode characters from console reads (I was using the DOS dir command). Thus I was returning some invalid filenames. Because of the typo, my script was only looking for zero size files... or invalid filenames which return zero size. The grouping was folder names with those characters.

Edited by Dana

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