Posted: 07:35pm 04 Nov 2013 |
Copy link to clipboard |
Print this post |
|
I wrote a small program (below) to generate a non-repeating list of random numbers in the range 1 to n.
Why? Well my car's audio player can play MP3 tracks contained in folders on a USB stick (or iPod) and it has a couple of random functions, but not the one I want.
I would like to randomly listen to all my favourite classical works without repeating until I have heard them all but the two random functions either randomise all tracks on the stick without regard to folder or randomly play all tracks within a folder. It cannot randomly select a folder and play all the tracks it contains in sequence. (We hate hearing only bits of a work.)
When not using the random function, it plays folders and their content in the sequence they were loaded onto the USB stick (regardless of folder name).
So the solution I adopted is to prefix the folder name with a random number on my desktop/laptop and when I use Win Explorer to copy them to the USB stick, they are copied in the prefix order (e.g. 001 Beethoven Symphony No 1, 002 Dvorak Cello Concerto, 003 Berlioz Symphony Fantastique, 004...).
To generate the non-repeating sequence, this program asks for the maximum number of folders to randomize (n), then produces a file consisting of n random numbers in the range 1 to n preceded by a sequence number. (This sequence number is only there for me to easily find my place again if I get interrupted whilst renaming the folders.)
All I do then is go through my un-prefixed alphabetical list of folder names in Win Explorer and prefix each one with the next random number. Because the random numbers don't repeat, I get to listen to the whole library before it repeats.
Method:
The program first asks for a number, the maximum number (n) of folders to randomise.
It generates a sequential list of numbers in the range 1 to n.
Then it:
- picks a number from the list at random and writes it to the file, preceded by a sequence number.
- removes that number from the list so that it isn't duplicated.
- repeats until the list is empty.
Job done.
Hugh
'Create non-repeating list of random numbers in range 1 - n
'Hugh Buckle October 2013
Input n 'Number range required
Dim numbers(n)
Open "RndNum1.csv" For output As #1 'Open output file
Populate.Array (n)
Print
Randomize Timer
ListSize=n
'Mainline which creates the CSV file of the form Sequence No, Random number
'suitable for loading into a spreadsheet.
Do
'Create a random index to access the array.
Index=Int(Rnd()*ListSize)+1
'Use the index to pick a number from the list of numbers remaining in the array
'and write it to the file, preceded by a sequence number.
Print #1, n-ListSize+1;",";Numbers(Index)
'Remove that randomly selected number from the array so that it is not repeated.
Close.up.numbers(Index,ListSize)
'Decrement the quantity of numbers left in the array.
ListSize=ListSize-1
'Loop until the array is empty
Loop Until ListSize=0
Close #1
End
'-------------------
Sub Populate.Array (n)
'Populates the array with sequence numbers 1 to n
For i = 1 To n
Numbers(i)=i
Next
End Sub 'Populate.Array
'-------------------
Sub Close.up.numbers(Index,ListSize)
'Removes an array element that has already been used
'so that the random numbers don't repeat.
For k=Index To ListSize-1
Numbers(k)=Numbers(k+1)
Next
Numbers(ListSize)=0
End Sub 'Close.up.numbers
|