How hard is it to create a not-so-random number generator? [closed]
Backstory:
So I was driving to band practice this evening. My car has a USB port where you can plug in a USB stick with MP3 files on it and the stereo will play them. I have about 100 MP3s on my stick so I pushed the 'Random' button. So from here to band practice, it played:
- Track 22
- Track 45
- Track 4
- Tr开发者_如何学Goack 11
- Track 87
- Track 66
- Track 98
Then on the way home, it played
- Track 16
- Track 27
- Track 33
And then I stopped at the petrol station. I filled up, got back in the car and the stereo fired up again. It played
- Track 22
- Track 45
- Track 4
- Track 11
- Track 87
I thought, WTF? What's with this 'random' generator? What are they using as a seed, if not time? Is a car stereo so memory-tight that it can't even use the C stdlib? Does anyone know how this kind of thing happens?
Without looking at the code of the music player it's hard to say. Some thoughts:
- It may be that it created a random playlist, then started the playlist again at the beginning. E.g., a random sequence which it then repeated, as opposed to independently coming up with the same sequence from its random number generator twice.
- Or, of course, it is using time as its seed and its clock is messed up and sometimes reading 0 or something, resulting in the repeated sequence.
- Or (with apologies) observational error. :-)
MP3 players tend to generate a random playlist when you select the random option, then play the same playlist.
The only 'random' part is that the playlist changes each time you re-set the 'random' option.
As an example, the iPod shuffle mp3 player has a functionality that allows you to re-shuffle the songs in order to generate a new 'random' playlist.
It's important to remember that without good statistical analysis our psychology lends to the fact that we spot patterns even when they don't exist (for all intents and purposes).
For example, if you had 10 songs on your iPod, there is a high chance when you play all ten songs in 2 sets, that there will be tracks next to each other in both sets. Our brains are wired to spot and notice that.
If the pattern you describe is true, then yes, they probably messed up their code a little. Perhaps the internal system time resets every time the device is powered off, so when you turn it on by pressing 'play all' instead of 'switch on' then 'play all' it might play the same tracks in the same order. Or perhaps time isn't used as a seed.
Or, perhaps (unlikely) it is random. A truly random sequence will repeat orders eventually, especially in such a small set of 100 it's not beyond the realms of possibility you will see repeats in subsets of the data.
It may be that the embedded device controlling the randomization doesn't have a clock. Perhaps it uses the number of tracks as seed or something similar.
If you don't have some external source for random seeds, such as readings from a clock, the software will, when all it's memory is reset, behave deterministically each time it is started.
精彩评论