Question:

Generating 5 random numbers between two numbers.?

by  |  earlier

0 LIKES UnLike

In ASP (classic asp)

how can I generate 5 unique random numbers (being each of them is greater than the previous one), between two numbers?

For example, I want to generate 5 numbers between 1 and 1000.

It must generate like these:

45-66-124-534-794

 Tags:

   Report

2 ANSWERS


  1. Although Jareh's solution will work, you'll notice that it will have a tendency to result in numbers that are close to the upper-bound.  To demonstrate, I just tried it a few times and got:

    1) 257 586 866 945 946

    2) 680 929 960 981 997

    3) 633 922 992 993 996

    4) 494 788 826 896 907

    5) 843 964 994 996 997

    6) 168 260 732 888 893

    7) 84 653 928 975 992

    The first value is nicely random, but as it goes on, you get progressively very high numbers.  In fact, the last value will be more than 900 about 91 percent of the time, when instead, the top number should be over 900 about 41 percent of the time.  In general with this method, you'll get numbers that are approximately in the ballpark of:

    500 750 875 938 969

    What you want to do instead is put the random numbers into an array and then sort the array.  I'm not an ASP guy so this may be full of bugs. If so, you should be able to fix them easily, but you want something like this:

    Randomize

    Dim Values(5),t,i,Tmp,Min,Count,Max

    Min = 0 '*** Minimum value

    Max = 1000 '*** Maximum value

    Count = 5 '*** Number of values

    for t= 1 to Count

      Values(t) = INT(((Max - Min) * Rnd) + Min)

    next

    '*** Ideally, you'd sort the Values array here.

    '*** Unfortunately, it looks like there isn't a

    '*** sort function in ASP? Hence, let's do it

    '*** ourselves. But really, you'd write a QuickSort

    '*** algorithm and use that.

    for t= 1 to Count-1

      for i= t+1 to Count

        if(Values(t) < Values(i) then

          Tmp = Values(t)

          Values(t) = Values(i)

          Values(i) = Tmp

        end if

      next

    next

    '*** Ok, now it's sorted, so we can print them out.

    for t= 1 to Count

      response.write (Values(t) & "<br>")

    next

    DaveE


  2. Voooooooaaaallllllaaaa :

    <%

    Randomize

    Dim LastMax,MIN,Num_of_Val,MAX,This_Rnd

    This_Rnd = 0

    Min = 0 '*** Minimum value

    Max = 1000 '*** Maximum value

    Num_of_Val = 5 '*** Number of value you want it to return

    for t= 1 to Num_of_Val

    do while This_Rnd <= MIn '*** loop until a greater value is returned

    This_Rnd = INT(((Max - MIN) * Rnd) + Min) '*** Generate the random value

    if t = 1 and This_Rnd >= ((Max - MIN) - (Num_of_Val + 1)) then This_Rnd = 0 '*** Check if the first return value will allow the application to the number of value requested

    loop

    Min = This_Rnd

    response.write (This_Rnd & "<br>")

    next

    %>

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions