Unique numbers in an array???

# Thread: Unique numbers in an array???

1. Dan
Senior Member
Join Date
Dec 1969
Posts
1,364

## Unique numbers in an array???

Hi,<BR><BR> I&#039m trying to create an array of random numbers but in doing so I would also like to be able to check and make sure that the array contains no elements that are duplicates - is there an easy way of doing this, I could weel simply be over complicating an easy procedure here&#039s the code that I have so far (below). If anyone could offer any suggestions / code I would be greatful for their help. Thanks in advance, Dan.<BR><BR>counter = 0<BR>nocheck=0<BR>do while counter &#060; total_records<BR> randomize<BR> radint = int(rnd*total_records)+1<BR> for i=0 to total_records-1<BR> if nocheck=0 then<BR> if rs_array(i) = radint then<BR> counter = counter - 1<BR> nocheck=1<BR> else<BR> rs_array(counter) = radint<BR> end if<BR> end if<BR> next<BR> nocheck=0<BR> counter = counter + 1<BR>loop

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Unique numbers in an array???

Aside from the fact that you should *NEVER* execute Randomize more than once per ASP page, what you have looks like the beginnings of the right idea. But you could make it somewhat more efficient:<BR><BR>Randomize &#039 only do this once!<BR>...<BR>Dim rsArray(9) &#039 10 random numbers...or whatever<BR>total_records = UBound(rsArray)<BR>counter = 0<BR>Do While counter &#060;= total_records<BR>&nbsp;&nbsp;&nbsp;&nbsp;radint = int(rnd*total_records)+1<BR>&nbsp;&nbsp;&nbsp;&nbs p;match = false<BR>&nbsp;&nbsp;&nbsp;&nbsp;For i = 0 To counter-1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;If radint = rsArray(i) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;match = true<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit For<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>&nbsp;&nbsp; &nbsp;&nbsp;If Not match Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;rsArray(counter) = radint<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;counter = counter + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>Loop<BR><BR>*********<BR><BR>Note that you do *NOT* bump the counter (or store the random value) *unless* there was no match found. That way the counter stays the same and you try again if there was a match found.<BR><BR>Also note that the FOR loop only checks the positions in the array that already have something stored in them! (So it checks nothing for the first number, for example.)<BR><BR>This technique is perfectly adequate for getting a handful of <BR>random numbers like this. Maybe up to 25 or so. If you need to get many more, then it is better to use one of the "Deck Shuffling" algorithms (so named because they do a good job of shuffling a deck of 52 cards, but they work pretty well for even a few hundred numbers).<BR><BR>Ask if you need such an algorithm. It is, of course, a little bit more complicated than the above.<BR><BR>

3. Dan
Senior Member
Join Date
Dec 1969
Posts
1,364

## RE: Unique numbers in an array???

Thanks for that Bill, If you could spare the time though, I would be greatful if you could expand the deck shuffling idea that you mentioned. What I&#039m actually heading for is selecting articles from three different tables within a database upon which I use the .RecordCount method which is where I get the value for total_records, I then want to loop through the recordset pulling out specific records - PLEASE do tell me if I&#039m going about this the wrong way or if there is a more efficient manner in which to go about this.<BR><BR>Thanks again, Dan.

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## How to shuffle a deck of cards

Well, the thing that matters is how many records you want to END UP WITH.<BR><BR>That is, whether you are selecting 10 records randomly from among 10 records or 10 records randomly from among 10,000 records, the code I showed you already is the right way to go.<BR><BR>Because you&#039ll never have to check more than 9 already-filled "slots" for a duplicate. In fact, the more records you are selecting from, the better the chances that you&#039ll never get a match that forces you to choose again.<BR><BR>So having said all that...<BR><BR>*IF* you want to "shuffle a deck" so that you can then display *ALL* the "cards" in the deck in random order, *then* you want the shuffling algorithm. <BR><BR>And it works like this:<BR><BR>(1) Set up one deck (array) in order, from 1 to N, however large your "deck" is.<BR>(2) Pick a card at random from the deck (e.g., pick a number from 1 to N) and get the card that is at that slot in the first deck and put it into any slot (so it might as well be slot 1 or slot N) in the output deck.<BR>(3) Take the LAST card in the input deck and move it to the slot where you just pulled out a card. <BR>(4) Change the size of the input deck! That is, do N = N - 1 !!<BR>(5) If N is greater than zero, go back to step 2.<BR><BR>Try to picture it before you then try to figure out this code:<BR><BR>&#060;%<BR>Function ShuffleDeck( deckSize )<BR>&nbsp;&nbsp;&nbsp;&nbsp;Dim inDeck(),outDeck()<BR>&nbsp;&nbsp;&nbsp;&nbsp;ReDi m inDeck( deckSize )<BR>&nbsp;&nbsp;&nbsp;&nbsp;ReDim outDeck( deckSize )<BR>&nbsp;&nbsp;&nbsp;&nbsp;For slot = 1 To deckSize<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;inDeck( slot ) = slot &#039 initialize the "cards" in first deck<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>&nbsp;&nbs p;&nbsp;&nbsp;For slot = deckSize To 1 Step -1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;randSlot = 1 + Int( slot * Rnd ) &#039 pick a random number from 1 to slot<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;outDeck(slot) = inDeck(randSlot) &#039 get a card from in, put it in out<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;inDeck(randSlot) = inDeck(slot) &#039 move last card into position just vacated<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>&nbsp;& nbsp;&nbsp;&nbsp;ShuffleDeck = outDeck<BR>End Function<BR>%&#062;<BR><BR>That&#039s it! Short and simple code! Be sure to call Randomize once on the ASP page before invoking the code. You can get a random "deck" of any size by simply doing:<BR><BR>&#060;%<BR>randomDeck = ShuffleDeck( 25 ) &#039 or any other number!<BR>%&#062;<BR><BR>Works quite well.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•