[thelist] HELP ASP gurus -- random numbers is hard

Anthony Baratta Anthony at Baratta.com
Tue Dec 5 15:29:13 CST 2000


PacBell has been mucking with my mail...I didn't see my answer come 
through. (also made a few edits since I got a "second chance")

At 12:40 PM 12/5/2000 -0500, you wrote:

>But seriously. All I want to do is put 10 unique random numbers into an
>array. I can get the array and I can get 10 numbers, but they're not all
>unique enough. I've tried some incredibly complicated nested loops, but
>I'm just generating errors with that. Please give a little if you can.

If you only need uniqueness between the 10 numbers:

Instead of an array use a dictionary object....

<%
    '' I use the Cap Rock Dictionary Object
       Set dictRandomList = Server.CreateObject("caprock.dictionary")
       varRandomListLength = 10
       varRandomSize = 1000

    '' This will iterate until you get varRandomListLength unique numbers
       Randomize
       while (dictRandomList.Count < varRandomListLength)
          dictRandomList(Int(CInt(varRandomSize)*Rnd)) = 
Int(CInt(varRandomSize)*Rnd)
       wend
%>

If you want uniqueness "forever":

If you have a database, compare each new random number against the database 
and stuff it into the database if its determined to be unique.


---
Anthony Baratta
President
Keyboard Jockeys





More information about the thelist mailing list