7 Replies Latest reply: Oct 13, 2012 7:25 AM by twtwtw
LongRiver Level 1 Level 1 (0 points)

This Applescript line

 

open some file of folder "[folderPathName]"

 

displays a random-choice jpg from a folder containing only jpgs, and works well – except that it always chooses the same “random” files, starting with one particular file and then going through the exact same sequence of “random” selections every time.

 

Restarting the Mac resets the sequence. The first time this line is called after a restart, it begins with that same first “random” selection and goes through the same sequence.

 

Am I misundersanding the some keyword? Does it need a “seed” to make it fully random?

 

Thank you.

  • Camelot Level 8 Level 8 (46,440 points)

    Is that your entire script?

     

    'some' should be random, and I can't seem to replicate your issue.

     

    The following script seems to return a pretty random item each time I run it:

     

    tell application "Finder" to set i to name of some item of (get files of (path to desktop))

    display dialog i

  • Pierre L. Level 5 Level 5 (4,140 points)

    Restarting the Mac resets the sequence. The first time this line is called after a restart, it begins with that same first “random” selection and goes through the same sequence.

     

    You're not the first one to experiment that. See this old thread.

  • LongRiver Level 1 Level 1 (0 points)

    Thank you.

     

    As far as I can tell, some will return the same sequence of "random" numbers starting with its first call after a system startup.

     

    So random number seems needed. It can work from a seed.

     

    This example isn't fully random, since the seed, current date, changes only once a second, but for many  purposes that's good enough:

     

    set n to random number from 1 to (count of [aFolder]) with seed (seconds of (current date))

  • Pierre L. Level 5 Level 5 (4,140 points)

    I don't know what's your version of Mac OS X, but here's what can be found in the AppleScript Release Notes about the bug fixes made in Mac OS X 10.3: “The arbitrary element reference form some item wasn’t very random. This has been fixed. [3044302]

  • LongRiver Level 1 Level 1 (0 points)

    Thanks for this. Something may have been fixed, but I find that some will always return the same sequence of files "randomly" chosen from a folder, starting fresh after a system startup.

  • Pierre L. Level 5 Level 5 (4,140 points)

    Unfortunately, I too am unable to replicate your issue on my MacBook Pro under OS X 10.8.2.

  • twtwtw Level 5 Level 5 (4,900 points)

    FIrst, try resetting the PRAM.  As far as I can think, the most likely cause of this effect would be that the system is seeding its random number generator with the exact same 32/64-bit integer at startup.  That speaks to something being off at a very low level, which a PRAM reset might shake loose.

     

    If that doesn't solve the problem, could you explain precisely what you do to make this happen - i.e., give us the whole script (it it's not too long) and tell us how and when it gets launched?  There are only three other ways I can think of to get this effect, and they are all far-fetched:

     

    1. You run this applescript so early in the startup process that the system hasn't been fully initialized.
    2. You've added a bad or outdated third-party scripting addition that's interfering with the way applescript works.
    3. You're running the command in a context that has (somehow) redefined the some keyword.

     

    I'm betting on the PRAM thing, though, because everything else is a bit crazy.