Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:41 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Non-Repeating Random Numbers

Author Message
shoebuckle
Senior Member

Joined: 21/01/2012
Location: Australia
Posts: 189
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
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 05:17pm 05 Nov 2013
Copy link to clipboard 
Print this post

Hmmmm.... this could be handy Hugh. My wife and I drive to Sydney (from Melbourne) four or five times a year to see the "kids". The randomizing that happens on the iTouch and another little "Creative" MP3 player we use could do with similar tweaking.

Greg
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024