Posted By

elightbo on 04/06/10


Tagged

array ASP


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

britevents


ASP Shuffle Array


 / Published in: ASP
 

  1. 'From http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=114
  2. Function Shuffle( inArray, needed )
  3. ' find out how many input elements there are...
  4. incnt = UBound( inArray )
  5. ' then create the output array to be the size
  6. ' requested via the "needed" argument
  7. dim outArray
  8. redim outArray( needed )
  9.  
  10. ' now we will select the number of values
  11. ' specified as "needed"...
  12. For i = 0 To needed
  13. ' choose a random number from 1 to our
  14. ' current input array usage size...
  15. choose = Int( incnt * Rnd(1) )
  16.  
  17. ' put that chosen element into the next
  18. ' slot in the output array...
  19. outArray( i ) = inArray( choose )
  20.  
  21. ' here's the tricky part: Since we just
  22. ' used the "choose" element, we don't need
  23. ' it any more...we replace it with the last
  24. ' element of the in-use part of the array!
  25. inArray( choose ) = inArray( incnt )
  26.  
  27. ' and then we (effectively) shrink the array!
  28. ' Next time through the loop, there will be
  29. ' one fewer elements in the array to choose
  30. ' from...because we have (effectively) deleted
  31. ' the one just chosen!
  32. incnt = incnt - 1
  33.  
  34. Next
  35. ' return the shuffled output
  36. Shuffle = outArray
  37. End Function

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ziye on April 7, 2010

He was the lights flowing away in the darkness. buy wow accountThese were the days of his wasted life; he saw a star fall from the sky

and disappeared, beijing escort Then he , who entered on covered with flowers, shanghai

escort to success and were of water and where devils and poisonous snakes hissed and crawled. shanghai escort But they had

made their way to success. yy2010.4.7

You need to login to post a comment.