random number generation

1. Senior Member
Join Date
Dec 1969
Posts
102

## random number generation

Does anyone know how to generate random numbers between 1-100 in ASP?

2. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## RE: random number generation

R&D --&#062; Randomize & Rnd

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: random number generation

&#060;%<BR>Randomize<BR>N = 1 + Int( Rnd * 100 )<BR>%&#062;<BR><BR>But I suspect that what you really meant was that you wanted to "shuffle a deck" of 100 numbers and then deal them out, so that no number is repeated? Yes?<BR><BR>I just happen to have a demo of doing just that...left over from answering this same question about six months ago, I think:<BR><BR>************* FILE: Shuffle.asp *************<BR>&lt;%<BR>&#039 This function will DEAL the "needed"<BR>&#039 number of values from the given "inArray"<BR>&#039<BR>&#039 If the value for "needed" matches the<BR>&#039 upper bound of the "inArray", then the<BR>&#039 entire "inArray" is dealt out.<BR>&#039<BR>&#039 NOTE: As written, this code never uses<BR>&#039 or touches element zero of the inArray<BR>&#039 and puts no value in element zero of the<BR>&#039 outArray. (Obviously, easy to change.)<BR>&#039<BR>Function Shuffle( inArray, needed )<BR>&nbsp;&nbsp;&nbsp;&nbsp;incnt = UBound( inArray )<BR>&nbsp;&nbsp;&nbsp;&nbsp;dim outArray<BR>&nbsp;&nbsp;&nbsp;&nbsp;redim outArray( needed )<BR>&nbsp;&nbsp;&nbsp;&nbsp;for i = 1 to needed<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;choose = Int( incnt * Rnd(1) ) + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;outArray( i ) = inArray( choose )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;inArray( choose ) = inArray( incnt )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;incnt = incnt - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;next<BR>&nbsp;&nbsp;& nbsp;&nbsp;Shuffle = outArray<BR>End Function<BR>%&#062;<BR><BR>&lt;HTML&#062;&lt;BODY& #062;<BR><BR>&lt;% <BR>Randomize<BR><BR>ar = Array(0,"you","can","put","anything","in","the","a rray","of","course")<BR>sh = Shuffle( ar, 4 )<BR>For i = 1 to 4<BR>%&#062;<BR>&nbsp;&nbsp;&nbsp;&nbsp;element &lt;% = i %&#062; is &lt;% =sh(i) %&#062;&lt;BR&#062;<BR>&lt;% <BR>Next <BR><BR>Response.Write "&lt;HR&#062;" & vbNewLine<BR><BR>&#039 what you are looking for...<BR>&#039 Just change 20 to 100 or whatever number<BR>Dim ar20(20)<BR>&#039 initialize the array elements to match<BR>&#039 the element number!<BR>For i = 1 To 20<BR>&nbsp;&nbsp;&nbsp;&nbsp;ar20(i) = i<BR>Next<BR>sh = Shuffle( ar20, 20 )<BR>For i = 1 To 20<BR>%&#062;<BR>&nbsp;&nbsp;&nbsp;&nbsp;element &lt;% = i %&#062; is &lt;% =sh(i) %&#062;&lt;BR&#062;<BR>&lt;% <BR>Next <BR>%&#062;<BR><BR>&lt;/BODY&#062;&lt;/HTML&#062;<BR><BR>******************************** <BR><BR>NOTE: The shuffling destroys the input array. If you need to preserve it, try specifying <BR>&nbsp; &nbsp; Function Shuffle( ByVal inArray, ByVal needed )<BR>

4. Senior Member
Join Date
Dec 1969
Posts
102

## AWESOME BILL!!! Thanks for the input

The first one was sufficient, but heah the second one will come in handy when I do a D&D dice rolling function.<BR><BR>All I want to do is rotate pactures and text in a random order.<BR><BR>AdRotator...I know...but I like doing things my way.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Want to know how shuffling works?

I thought I&#039d put in more comments, in an effort to explain how the Shuffle function works.<BR><BR>Then, there is a small example at the end.<BR><BR>********** code for the Shuffle routine **************<BR>&lt;%<BR>&#039 This function will DEAL the "needed"<BR>&#039 number of values from the given "inArray"<BR>&#039<BR>&#039 If the value for "needed" matches the<BR>&#039 upper bound of the "inArray", then the<BR>&#039 entire "inArray" is dealt out.<BR>&#039<BR>&#039 NOTE: As written, this code never uses<BR>&#039 or touches element zero of the inArray<BR>&#039 and puts no value in element zero of the<BR>&#039 outArray. (Obviously, easy to change.)<BR>&#039<BR>Function Shuffle( inArray, needed )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 find out how many input elements there are...<BR>&nbsp;&nbsp;&nbsp;&nbsp;incnt = UBound( inArray )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 then create the output array to be the size<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 requested via the "needed" argument<BR>&nbsp;&nbsp;&nbsp;&nbsp;dim outArray<BR>&nbsp;&nbsp;&nbsp;&nbsp;redim outArray( needed )<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 now we will select the number of values<BR>&nbsp;&nbsp;&nbsp;&nbsp;&#039 specified as "needed"...<BR>&nbsp;&nbsp;&nbsp;&nbsp;for i = 1 to needed<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&#039 choose a random number from 1 to our<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&#039 current input array usage size...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;choose = Int( incnt * Rnd(1) ) + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&#039 put that chosen element into the next<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&#039 slot in the output array...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;outArray( i ) = inArray( choose )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&#039<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&#039 here&#039s the tricky part: Since we just<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&#039 used the "choose" element, we don&#039t need<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&#039 it any more...we replace it with the last<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&#039 element of the in-use part of the array!<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;inArray( choose ) = inArray( incnt )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&#039 and then we (effectively) shrink the array!<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&#039 Next time through the loop, there will be<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&#039 one fewer elements in the array to choose<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&#039 from...because we have (effectively) deleted<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&#039 the one just chosen!<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;incnt = incnt - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;Next<BR>&nbsp;&nbsp;& nbsp;&nbsp;Shuffle = outArray<BR>End Function<BR>%&#062;<BR>**********************<BR>< BR>An example. Let&#039s Shuffle a small array of four items.<BR><BR>&#060;%<BR>ar = Array("--not used--","one","two","three","four")<BR>sh = Shuffle( ar, 4 ) &#039 return all 4 values, in mixed order.<BR>%&#062;<BR><BR>So look what happens:<BR><BR>(1) We start with "incnt" set to 4, the size of the input array.<BR>(2) "needed" is also 4, so...<BR>(3) We create the output array with 4 elements in it (well, 5 if you include the unused 0 element).<BR>(4) First time in the loop: We choose a random number from 1 to 4. Let&#039s say we chose 2.<BR>(5) So the value of input element 2 (which is "two") goes into slot 1 of the output array.<BR>(6) Then we copy the "incnt" element of the input array (which is "four") to slot 2 of the input array. AND...<BR>(7) We change "incnt" from 4 to 3.<BR><BR>At this point, we are ready to ignore element 4 of the input array. So we are effectively working with an array that is now:<BR>&nbsp; &nbsp; &nbsp; &nbsp; "one","four","three"<BR><BR>(8) We loop back to step 4.<BR>(4.b) Since "incnt" is now 3, we choose a random number from 1 to 3. Let&#039s say we chose 3! (To demonstrate that the technique works even when you choose the last element.)<BR>(5.b) So the value of input element 3 (which is "three") goes into slot 2 of the output array.<BR>(6.b) Then we copy the "incnt" element of the input array (which is "three") to slot 3 of the input array (which, of course, really does nothin). AND...<BR>(7.b) We change "incnt" from 3 to 2. Thus making the working array now be, effectively:<BR>&nbsp; &nbsp; &nbsp; &nbsp; "one","four"<BR>(8.b) We loop back to step 4.<BR>(4.c) Since "incnt" is now 2, we choose a random number from 1 to 2. Let&#039s say we chose 1!<BR>(5.c) So the value of input element 1 (which is "one") goes into slot 3 of the output array.<BR>(6.c) Then we copy the "incnt" element of the input array (which is "four") to slot 1 of the input array.<BR>(7.c) We change "incnt" from 2 to 1. Thus making the working array now be, effectively:<BR>&nbsp; &nbsp; &nbsp; &nbsp; "four"<BR>(8.c) We loop back to step 4.<BR>(4.d) Since "incnt" is now 1, we choose a random number from 1 to 1. Ummm...maybe we should choose 1??<BR>(5.d) So the value of input element 1 (which is "four") goes into slot 4 of the output array.<BR>(6.d) Then we copy the "incnt" element of the input array (which is "four") to slot 1 of the input array (which, of course, really does nothin). AND...<BR>(7.d) We change "incnt" from 1 to 0. Thus finishing off the input array.<BR>(8.d) The loop is done.<BR><BR>And, looking at the output array and the order in which the elements were put there, we have (as the returned value of the function):<BR><BR>&nbsp; &nbsp; Shuffle = Array( empty, "two", "three", "one", "four" )<BR><BR>Ta-da! Your mileage may vary.<BR><BR><BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
102

## Thanks for the explanation!

You have helped me too many times to mention...<BR><BR>May God bless you here and in the afterlife...<BR><BR>I&#039m starting to learn a little here and there, but I guess the next thing I need to get is the INNER JOIN thing and learning how to open and combine multiple tables.<BR><BR>Also need to get training on sub tables...<BR><BR>Remember a while back when you gave me that link to scroll window that allowed you to see the title in one box and description in the other...well DA me never could get it.<BR><BR>It was the subTable thing that had me confused. I couldn&#039t figure out if I needed to create a subtable or not...<BR><BR>Anyway thanks for the help man...REALLY!

7. Senior Member
Join Date
Dec 1969
Posts
102

## Okay...I guess I need to utilize help files more

Good point Akhilesh...<BR><BR>I need to utilize the "help" files more often...<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
•