
Unique numbers in an array???
Hi,<BR><BR> I'm 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's 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 < total_records<BR> randomize<BR> radint = int(rnd*total_records)+1<BR> for i=0 to total_records1<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

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 ' only do this once!<BR>...<BR>Dim rsArray(9) ' 10 random numbers...or whatever<BR>total_records = UBound(rsArray)<BR>counter = 0<BR>Do While counter <= total_records<BR> radint = int(rnd*total_records)+1<BR> &nbs p;match = false<BR> For i = 0 To counter1<BR> &nb sp;If radint = rsArray(i) Then<BR> match = true<BR> Exit For<BR> & nbsp;End If<BR> Next<BR> If Not match Then<BR> rsArray(counter) = radint<BR> &nbs p; counter = counter + 1<BR> 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>

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'm 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'm 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.

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'll never have to check more than 9 alreadyfilled "slots" for a duplicate. In fact, the more records you are selecting from, the better the chances that you'll 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><%<BR>Function ShuffleDeck( deckSize )<BR> Dim inDeck(),outDeck()<BR> ReDi m inDeck( deckSize )<BR> ReDim outDeck( deckSize )<BR> For slot = 1 To deckSize<BR> &n bsp; inDeck( slot ) = slot ' initialize the "cards" in first deck<BR> Next<BR> &nbs p; For slot = deckSize To 1 Step 1<BR> &nb sp;randSlot = 1 + Int( slot * Rnd ) ' pick a random number from 1 to slot<BR> outDeck(slot) = inDeck(randSlot) ' get a card from in, put it in out<BR> & nbsp;inDeck(randSlot) = inDeck(slot) ' move last card into position just vacated<BR> Next<BR> & nbsp; ShuffleDeck = outDeck<BR>End Function<BR>%><BR><BR>That's 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><%<BR>randomDeck = ShuffleDeck( 25 ) ' or any other number!<BR>%><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

Forum Rules

