AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Random Screensaver

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
antonyb



Joined: 26 May 2004
Posts: 61

PostPosted: Tue Jan 24, 2006 11:46 am    Post subject: Random Screensaver Reply with quote

Hello,

Just a quick hack to make Windows choose a different, random, screensaver each time it launches one. Compile up the following script, then rename the .exe as something like "Random Screensaver.scr", copy it into your C:\Windows\system32 directory (or equivalent), and select it as your default screensaver.

In order to configure a particular screensaver, you'll need to set it as your default, configure it, then set Random Screensaver back as the default.

Code:
#NoTrayIcon

Loop, %A_WinDir%\system32\*.scr
{
  Random, r, 0.0, %A_Index%
  if (r<1)
    ss=%A_LoopFileFullPath%
}

RunWait, %ss% %1% %2%


NB: I leave it as an exercise for the reader to figure out how the random file picking algorithm works Smile (answer here).

Edit: Run should be RunWait
Back to top
View user's profile Send private message
POINTS



Joined: 18 Jan 2006
Posts: 284

PostPosted: Thu Jan 26, 2006 12:49 am    Post subject: Reply with quote

What does the %1% and the %2% do? Are they inputs to the script?

I'm trying to do something a little simplier. I just want to run a screensaver with a push of a button when I leave my computer. But, whenever I try to run a screensaver an options menu for it comes up instead. Any hints?

Code:

RunWait, "C:\windows\boinc.scr"
Back to top
View user's profile Send private message Visit poster's website
antonyb



Joined: 26 May 2004
Posts: 61

PostPosted: Thu Jan 26, 2006 9:16 am    Post subject: Reply with quote

Hi,

Quote:
What does the %1% and the %2% do? Are they inputs to the script?

Exactly.

Quote:
I'm trying to do something a little simplier. I just want to run a screensaver with a push of a button when I leave my computer. But, whenever I try to run a screensaver an options menu for it comes up instead. Any hints?

Its always worth searching these forums, you know Smile Take a look at this thread.

Ant.
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Thu Jan 26, 2006 12:55 pm    Post subject: Reply with quote

Good answer...
I use Chris' suggestion with the following lines:
Code:
;--- Ctrl+Alt+S: screensaver launcher
^!s::
   PostMessage 0x0112, 0xF140, 0,, Program Manager ; 0x0112 is WM_SYSCOMMAND -- 0xF140 is SC_SCREENSAVE
return
in my permanent script. It works flawlessly.

antonyb, I didn't knew this algorithm, but it is very interesting and quite smart. I was quite skeptical at first, but the explainations are convincing...
Very good script, that can be tweaked at will.
For example, I can put a list of wanted screen savers, including in other folders, at the start of the script, and perhaps give each one a note to increase the chances to be chosen.


Last edited by PhiLho on Wed Jun 07, 2006 11:28 am; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
antonyb



Joined: 26 May 2004
Posts: 61

PostPosted: Thu Jan 26, 2006 2:23 pm    Post subject: Reply with quote

Thanks PhiLho - glad you like it.

Quote:
For example, I can put a list of wanted screen savers, including in other folders, at the start of the script, and perhaps give each one a note to increase the chances to be chosen.

I actually do something similar to this. I have a list of heavy-duty (OpenGL, CPU intensive) screensavers for when my laptop is on AC, and a list of light screensavers (very low CPU use) for when its on battery, and automatically switch between the lists depending on the power status. Its being able to do stuff like that which makes me absolutely love AutoHotkey Very Happy

Which reminds me, must post my Power Status script...

Ant.
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Wed Jun 07, 2006 1:20 pm    Post subject: Reply with quote

The link antonyb gave is dead... It was probably an unauthorized copy...
I found another copy of this entry to O'Reilly's Perl Cookbook: Picking a Random Line from a File. You might get warning from your browser for expired SSL certificate, go ahead, that's not your bank! Wink
In case this page disappears too, here it the explanation of the algorithm:
Quote:
This is a beautiful example of a solution that may not be obvious. We read every line in the file but don't have to store them all in memory. This is great for large files. Each line has a 1 in N (where N is the number of lines read so far) chance of being selected.
[...]
If you know line offsets (for instance, you've created an index) and the number of lines, you can randomly select a line and jump to its offset in the file, but you usually don't have such an index.

Here's a more rigorous explanation of how the algorithm works. The function call rand ($.) picks a random number between 0 and the current line number. Therefore, you have a one in N chance, that is, 1/N, of keeping the Nth line. Therefore you've a 100% chance of keeping the first line, a 50% chance of keeping the second, a 33% chance of keeping the third, and so on. The question is whether this is fair for all N, where N is any positive integer.

First, some concrete examples, then abstract ones.

Obviously, a file with one line (N=1) is fair: you always keep the first line because 1/1 = 100%, making it fair for files of 1 line. For a file with two lines, N=2. You always keep the first line; then when reaching the second line, you have a 50% chance of keeping it. Thus, both lines have an equal chance of being selected, which shows that N=2 is fair. For a file with three lines, N=3. You have a one-third chance, 33%, of keeping that third line. That leaves a two-thirds chance of retaining one of the first two out of the three lines. But we've already shown that for those first two lines there's a 50-50 chance of selecting either one. 50 percent of two-thirds is one-third. Thus, you have a one-third chance of selecting each of the three lines of the file.

In the general case, a file of N+1 lines will choose the last line 1/(N+1) times and one of the previous N lines N/(N+1) times. Dividing N/(N+1) by N leaves us with 1/(N+1) for each the first N lines in our N+1 line file, and also 1/(N+1) for line number N+1. The algorithm is therefore fair for all N, where N is a positive integer.

We've managed to choose fairly a random line from a file with speed directly proportional to the size of the file, but using no more memory than it takes to hold the longest line, even in the worst case.

_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Titan



Joined: 11 Aug 2004
Posts: 5067
Location: imaginationland

PostPosted: Wed Jun 07, 2006 4:45 pm    Post subject: Reply with quote

PhiLho wrote:
The link antonyb gave is dead...
If you're really interested here is the link: http://web.archive.org/web/20040515201322/http://www.unix.org.ua/orelly/perl/cookbook/ch08_07.htm
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
AL2
Guest





PostPosted: Sun Jul 01, 2007 4:53 pm    Post subject: Reply with quote

Very Happy this is pretty good,but how do i set it to random "Play-sound" instead of screen savers? Confused
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group