You have omitted the tell application "System Events" block from the code you found.
In Applescript some commands exist in the dictionary of specific applications and must be referenced with a 'tell application' block. In this case the 'every desktop' call is in the "System Events" app.
Try this.
tell application "Finder"
set desktopPictures to folder "Path:To:Desktop:Pictures:"
set fileList to name of every file of desktopPictures
tell application "System Events"
set theDesktops to a reference to every desktop
end tell
repeat with aDesktop in theDesktops
set theNum to random number from 1 to (count fileList) with seed ((time of (current date)) * 4)
set fileName to item theNum of fileList
set picture of aDesktop to file fileName in desktopPictures
end repeat
end tell