Results 1 to 6 of 6

Thread: Fastest random number list...

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    Question Fastest random number list...

    I want to establish a list of random numbers, corresponding to a database question list. The problem is that I may have 10 questions, but non-consecutive numbers (thanks to the Access autonumber field), and hence this could take some time as the number of questions increases.

    Usually, I would just keep generating random numbers in the range of lowest to highest number, check if a question exists for that number, and then check if that number has already been put in the list (or perhaps those last 2 in reverse order). What would be a faster way? This is for an internet thingy, so speed is the key here. I am building these routines into a .dll for a set of ASP to access.

    -RJ
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  2. #2

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Ok, how about this: I get a list of all the available question numbers, then swap each element in turn with another random element in the list. Can anyone confirm the randomness of such an approach?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  3. #3
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Unhappy may or may not help

    I'm not sure I quite understand what you want, but let me take a shot:
    If you have a bunch of non-consecutive numbers, store them in an array which has consecutive indecies. Ex:
    anArray(0) = 1
    anArray(1) = 10
    anArray(2) = 24
    blah blah blah

    Then to select one of the non-consecutive values, generate a random number from 0 to Ubound(anArray). You do this and get the value in one step:
    nonconsecutive# = anArray(Math.round(Rnd * Ubound(anArray),0))

    Tell me if that is what you want.
    jmiller

    BTW: I have to warn you about generating a whole random number using Math.round(num,0). The first and last numbers will not generate as often as the middle numbers. This is because of rounding. '0' will only be generated when the decimal number is >=0<.5 '1', however, will be generated if the decimal number is >=.5<1.5 The solution is kinda long so ask me if you need it.

  4. #4

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Yeah, that's basically the approach I'm taking - I just swap array elements randomly. Cheers.

    -RJ
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Another Possible Tweak

    The random number generator itself may not be the fastest option. Keep in mind the possibility that you could pre-generate the random numbers. If you create a list of random numbers before they are actually needed (if there is a point which isn't quite time-critical), then you could just use the next random number in the list rather than create a new random number each time. After all, just because it was generated a while back, it's still random.

  6. #6

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Yeah, could do that. But I think in this application, the random generation only happens once, and I've tested it this way, and it's fast enough for my purposes.

    Cheers,
    RJ
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

Posting Permissions

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



Click Here to Expand Forum to Full Width