True Randomization

XSYLUS

Member
Joined
Jan 25, 2008
Messages
5
Programming Experience
Beginner
Greetings,

This may have been asked before but I figured I'd throw it out there and see who takes the bait.

I know that computers "technically" do not do random and as a result I've been attempting to work on a program that improves on that limitation.

I've created a simple program using the "roll of a single die" type code where it pulls numbers from 1 to 6 and displays a "random" number everytime the button is clicked.

I've modified that code to show all 6 numbers in a list (or array) like so.

1
2
3
4
5
6

For testing purposes the program displays the above numbers in numerical order as show above, then once the button is clicked it "shuffles" those numbers arround... except, not really. One of the aspects I'm trying to implement into this program is avoiding duplicate numbers.

For example, I want to avoid this type of randomization:

6
2
1
6
4
3
3

As you can see both "6" and "3" are used twice and "4" and "5" are never used.

I basically want to make a program that purposely uses every number only once.

Furthermore the concept is to be able to use such code in an mp3 player so that you can have a bunch of tracks that you want to shuffle and play in a "random" order and you do NOT have to hear the same track twice.

This means the code has to be dynamic enough to detect when new tracks have been added while a track is playing and to "redimention" and include those tracks in the shuffle.

I was hoping to solve this on my own but my programming knowledge and experience is, unfortunately, sub par.

I've been able to write some good programs with the minimal skills I have but for the most part I learn by trial and error and I only vaguely understand some of the functions I'm using.
 
One way to go would be to use a List(of T) You can use a the random number generator in the framework to get you number but once a track is played you can remove that number from the list.. and place into another list called "Played" or somethig, therefore it only gets played once. Adding to a List(of T) is easy as it self adjusting with regrads to it's size.

With me?
 
I've seen List(of T) used before but I never quite grasped the full concept of it. I looked it up after you mentioned it but I'm still not sure how exactly it applies and can be used in my scenario. If you could elaborate on this that would be great. Thanks for your time.
 
I've already did a similar thing on my lottery numbers generator. It always get 6 different numbers from 1 to 45. What I did was a random-then-compare-approach such as after doing a random for the next result, I check to see if it is equal to the previous then do a random if necessary. For your sample it will be like this:
1) random then store first result
2) random then compare to first result. if equal then random again
3) and so on...

This will ensure that your numbers will never duplicate. Anyways, I tried the lottery using the unique combinations my program generated, I didn't win anything hehe.
 
I've seen List(of T) used before but I never quite grasped the full concept of it. I looked it up after you mentioned it but I'm still not sure how exactly it applies and can be used in my scenario. If you could elaborate on this that would be great. Thanks for your time.
List(Of T) is a collection, a managed array that simplifies all common array operations. The T is the Type you want to limit the collection to, for example String or Integer, this is the treat of generics; no type casting is needed when interfacing this collection. Using a collection for your "unique randoms" case is like pulling a ticket out of a hat, once drawn it is no longer among the remaining options, it's a O(1) operation. What Never suggested I recommend not doing, you could be locking up the application while it generates random numbers and never finding the last remaining "ticket". Here is a sample implementation of what Vis781 suggested:
VB.NET:
Private r As New Random

Function GetRandoms() As Integer()
    Dim options As New List(Of Integer)
    options.AddRange(New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9})
    Dim randomlist As New List(Of Integer)
    Do Until options.Count = 0
        Dim ix As Integer = r.Next(0, options.Count)
        randomlist.Add(options(ix))
        options.RemoveAt(ix)
    Loop
    Return randomlist.ToArray
End Function
 
For those on Studio 2008 LINQ offers a beautiful new alternative to generating this list:

VB.NET:
Dim rnd As New System.Random()
Dim numbers = Enumerable.Range(1, 100). _
  OrderBy(Function() rnd.Next)

That's it!

More details on this functionality here: Advanced Basics: The LINQ Enumerable Class, Part 1

Edit:

To elaborate, the above gets you the list of random numbers. You could then of course .ToList the numbers IEnumerable and do all the other things you talked about (like one time consumption and then removal from the List).
 
Last edited:
Back
Top