
random number generation
Does anyone know how to generate random numbers between 1100 in ASP?

RE: random number generation
R&D > Randomize & Rnd

RE: random number generation
<%<BR>Randomize<BR>N = 1 + Int( Rnd * 100 )<BR>%><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><%<BR>' This function will DEAL the "needed"<BR>' number of values from the given "inArray"<BR>'<BR>' If the value for "needed" matches the<BR>' upper bound of the "inArray", then the<BR>' entire "inArray" is dealt out.<BR>'<BR>' NOTE: As written, this code never uses<BR>' or touches element zero of the inArray<BR>' and puts no value in element zero of the<BR>' outArray. (Obviously, easy to change.)<BR>'<BR>Function Shuffle( inArray, needed )<BR> incnt = UBound( inArray )<BR> dim outArray<BR> redim outArray( needed )<BR> for i = 1 to needed<BR> &nbs p; choose = Int( incnt * Rnd(1) ) + 1<BR> &nb sp;outArray( i ) = inArray( choose )<BR> &nb sp;inArray( choose ) = inArray( incnt )<BR> &nb sp;incnt = incnt  1<BR> next<BR> & nbsp; Shuffle = outArray<BR>End Function<BR>%><BR><BR><HTML><BODY& #062;<BR><BR><% <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>%><BR> element <% = i %> is <% =sh(i) %><BR><BR><% <BR>Next <BR><BR>Response.Write "<HR>" & vbNewLine<BR><BR>' what you are looking for...<BR>' Just change 20 to 100 or whatever number<BR>Dim ar20(20)<BR>' initialize the array elements to match<BR>' the element number!<BR>For i = 1 To 20<BR> ar20(i) = i<BR>Next<BR>sh = Shuffle( ar20, 20 )<BR>For i = 1 To 20<BR>%><BR> element <% = i %> is <% =sh(i) %><BR><BR><% <BR>Next <BR>%><BR><BR></BODY></HTML><BR><BR>******************************** <BR><BR>NOTE: The shuffling destroys the input array. If you need to preserve it, try specifying <BR> Function Shuffle( ByVal inArray, ByVal needed )<BR>

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.

Want to know how shuffling works?
I thought I'd 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><%<BR>' This function will DEAL the "needed"<BR>' number of values from the given "inArray"<BR>'<BR>' If the value for "needed" matches the<BR>' upper bound of the "inArray", then the<BR>' entire "inArray" is dealt out.<BR>'<BR>' NOTE: As written, this code never uses<BR>' or touches element zero of the inArray<BR>' and puts no value in element zero of the<BR>' outArray. (Obviously, easy to change.)<BR>'<BR>Function Shuffle( inArray, needed )<BR> ' find out how many input elements there are...<BR> incnt = UBound( inArray )<BR> ' then create the output array to be the size<BR> ' requested via the "needed" argument<BR> dim outArray<BR> redim outArray( needed )<BR><BR> ' now we will select the number of values<BR> ' specified as "needed"...<BR> for i = 1 to needed<BR> &nbs p; ' choose a random number from 1 to our<BR> & nbsp;' current input array usage size...<BR> &nb sp; choose = Int( incnt * Rnd(1) ) + 1<BR> &nb sp;' put that chosen element into the next<BR> ' slot in the output array...<BR> &n bsp; outArray( i ) = inArray( choose )<BR> &nb sp;'<BR> &n bsp; ' here's the tricky part: Since we just<BR> ' used the "choose" element, we don't need<BR> ' it any more...we replace it with the last<BR> ' element of the inuse part of the array!<BR> &nbs p; inArray( choose ) = inArray( incnt )<BR> &nb sp;' and then we (effectively) shrink the array!<BR> &nbs p; ' Next time through the loop, there will be<BR> &n bsp;' one fewer elements in the array to choose<BR> &nbs p; ' from...because we have (effectively) deleted<BR> &nb sp; ' the one just chosen!<BR> &nb sp; incnt = incnt  1<BR> Next<BR> & nbsp; Shuffle = outArray<BR>End Function<BR>%><BR>**********************<BR>< BR>An example. Let's Shuffle a small array of four items.<BR><BR><%<BR>ar = Array("not used","one","two","three","four")<BR>sh = Shuffle( ar, 4 ) ' return all 4 values, in mixed order.<BR>%><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's 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> "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's 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> "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's 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> "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> Shuffle = Array( empty, "two", "three", "one", "four" )<BR><BR>Tada! Your mileage may vary.<BR><BR><BR><BR><BR>

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'm 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't figure out if I needed to create a subtable or not...<BR><BR>Anyway thanks for the help man...REALLY!

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

Forum Rules

