I have this code in Visual Basic that randomly changes wallpaper on the first boot of the day:
Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer)) Sub Main() Dim WallpaperNumNew As Integer Dim WallpaperCurrent As Integer Dim WallpaperLastChgDate As Date Dim LoopNum As Integer On Error Resume Next 'if neither key exists, ignore error. They will get created at program end. WallpaperLastChgDate = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Control Panel\Desktop", "WallpaperLastUpdate", Nothing) 'get the date the wallpaper was last changed WallpaperCurrent = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Control Panel\Desktop", "WallpaperNumber", Nothing) 'get current wallpaper number On Error GoTo 0 If WallpaperLastChgDate = Today() Then Exit Sub 'prevent changing wallpaper on every reboot during the day Do WallpaperNumNew = GetRandomNumber(1, 7) LoopNum = LoopNum + 1 If LoopNum > 20 Then Exit Do 'prevent infinite loop Loop While WallpaperNumNew = WallpaperCurrent 'if current and new are the same, loop until they are not Select Case WallpaperNumNew Case 1 My.Computer.FileSystem.CopyFile("C:\Windows\Web\WallPaper\MyWallPaper\JPEGs\Wallpaper1.jpg", "C:\Users[username]\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg", FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing) Case 2 My.Computer.FileSystem.CopyFile("C:\Windows\Web\WallPaper\MyWallPaper\JPEGs\Wallpaper2.jpg", "C:\Users[username]\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg", FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing) Case 3 My.Computer.FileSystem.CopyFile("C:\Windows\Web\WallPaper\MyWallPaper\JPEGs\Wallpaper3.jpg", "C:\Users[username]\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg", FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing) Case 4 My.Computer.FileSystem.CopyFile("C:\Windows\Web\WallPaper\MyWallPaper\JPEGs\Wallpaper4.jpg", "C:\Users[username]\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg", FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing) Case 5 My.Computer.FileSystem.CopyFile("C:\Windows\Web\WallPaper\MyWallPaper\JPEGs\Wallpaper5.jpg", "C:\Users[username]\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg", FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing) Case 6 My.Computer.FileSystem.CopyFile("C:\Windows\Web\WallPaper\MyWallPaper\JPEGs\Wallpaper6.jpg", "C:\Users[username]\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg", FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing) Case 7 My.Computer.FileSystem.CopyFile("C:\Windows\Web\WallPaper\MyWallPaper\JPEGs\Wallpaper7.jpg", "C:\Users[username]\AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg", FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing) Case Else Exit Sub 'do nothing End Select My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop", "WallpaperNumber", WallpaperNumNew) 'write new wallpaper number to registry My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\Desktop", "WallpaperLastUpdate", Today()) 'write new wallpaper change date to registry End Sub Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer ' Returns a random number between the optional Low and High parameters ' from: http://www.developerfusion.com/code/3940/random-numbers-that-work/ Return objRandom.Next(Low, High + 1) End Function
The program is set to run via Task Manager and works great, but since it is random, I often get the same wallpaper two or three days later. Can anyone think of a way of randomly cycling though the seven available wallpapers so that there are no duplicates in the series?
I'm not looking for actual code but just some way of tracking what wallpapers have previously been chosen and then eliminating those from the available pool until all seven have been selected. (Yeah, the last would not be random.)
I have written another program that can select a specific wallpaper based on the day of the week, but then I'd get the same wallpaper on each day, (i.e. every Monday would be the same wallpaper), which would be kind of boring.
Any ideas would be welcome. Thanks
Edit: I think I may have thought of a solution. The first time the code runs it generates a list of seven random numbers of values 1 - 7, say: 4, 7, 3, 1, 5, 2, 6 and stores them in a registry setting as: 4731526. Then each time the code runs (once a day) it retrieves another value stored in the registry that indicate what cycle number it's on, uses that position number from the random string, and then increments the cycle number. So in my example above, on the first day it uses wallpaper 4, then 7, then 3, etc. When it gets to 6 (the 7th value), it generates a new set of random numbers, stores it in the registry and sets the cycle number back to one, and the cycle start over with a new, different random string.
If this works, I'll post it as an answer as well as the code.
EDIT 2: I believe I have the code to accomplish what I'm after. I have run it once, and it has generated a seven-digit random number. I just need to wait until day eight to see if the code generates a new list of randomly arranged seven digits.