Fun with random numbers

# Thread: Fun with random numbers

1. Kentium Guest

## Fun with random numbers

I&#039;m trying to pick 3 of 4 possible scenarios, with one scenario always required-<BR><BR>scenario1 - required<BR>scenario2<BR>scenario3<BR>scenario4<BR ><BR>I&#039;m using this code-<BR><BR>DIM IndexSlot1,IndexSlot2,IndexSlot3<BR>Randomize &#039; Initialize random-number generator.<BR>IndexSlot1=Int((1 * Rnd) + 1)<BR>IndexSlot2=Int((4 * Rnd) + 1)<BR> do while IndexSlot2 = "1" <BR> IndexSlot2=Int((4 * Rnd) + 1)<BR> loop<BR>IndexSlot3=Int((4 * Rnd) + 1)<BR> do while IndexSlot3 = "1"<BR> IndexSlot3=Int((4 * Rnd) + 1)<BR> if IndexSlot3 &#060;&#062; IndexSlot2 then<BR> exit do<BR> end if<BR> loop<BR><BR>I&#039;m trying to make it so that index 2 and 3 don&#039;t equal the required index, or each other.<BR><BR>I think i&#039;m close...but for some reason I still get index 2 and 3 to come up the same on occasion.<BR><BR>Ideas?

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

## Did you see my post the other day??

About the ASPFAQs, category Math Functions and the function that "shuffles and deals" a deck of "cards"??<BR><BR>Anyway, I see you&#039;re trying to make this simpler, but you&#039;ve got a pair of goofs.<BR><BR>Comments preceded by ###<BR><BR>&#060;%<BR>DIM IndexSlot1,IndexSlot2,IndexSlot3<BR>Randomize &#039; Initialize random-number generator.<BR>### ummm...why do you use RND for IndexSlot1???<BR>### If it always has to be 1, why not just set it so:<BR>IndexSlot1=Int((1 * Rnd) + 1) &#039; original<BR><BR>### If you don&#039;t want to pick 1, then why have your low number<BR>### range start with 1???<BR>IndexSlot2=Int((4 * Rnd) + 1)<BR>### don&#039;t test vs. "1" ... it works, but that makes VBS <BR>### do the extra work of converting the *number* in IndexSlot2<BR>### into a string in order to do the work<BR>do while IndexSlot2 = "1" <BR>&nbsp; &nbsp; IndexSlot2=Int((4 * Rnd) + 1)<BR>loop<BR>### same comment about IndexSlot3... why start at 1 if you don&#039;t want 1?<BR>IndexSlot3=Int((4 * Rnd) + 1)<BR>### Think about the DO WHILE for a second:<BR>### Suppose that IndexSlot3 is 3 from above pick.<BR>### Will the loop even EXECUTE???<BR>###<BR>### Of course not! IndexSlot3 is *already* not = "1",<BR>### so it doesn&#039;t do the loop even once!<BR>### So if IndexSlot2 had a 3 in it... you never check<BR>### the one against the other, so they match!<BR>do while IndexSlot3 = "1"<BR>&nbsp; &nbsp; <BR>&nbsp; &nbsp; IndexSlot3=Int((4 * Rnd) + 1)<BR>&nbsp; &nbsp; if IndexSlot3 &#060;&#062; IndexSlot2 then<BR>&nbsp; &nbsp; &nbsp; &nbsp; exit do<BR>&nbsp; &nbsp; end if<BR>loop<BR>%&#062;<BR><BR>********************* ******<BR><BR>So, let&#039;s start over:<BR><BR>&#060;%<BR>Dim indexSlot1, indexSlot2, indexSlot3<BR>Randomize<BR><BR>&#039; you *must* have a 1 you said, so why not just assign it:<BR>indexSlot1 = 1<BR><BR>&#039; now don&#039;t include the value 1 in the possible values picked<BR>&#039; for slots 2 and 3!<BR>indexSlot2 = Int( Rnd() * 3 ) + 2 &#039; gets 2, 3, or 4<BR><BR>&#039; we shorten the code a little using DO...LOOP WHILE instead<BR>&#039; of using DO WHILE...LOOP:<BR><BR>Do <BR>&nbsp; &nbsp; indexSlot3 = Int( Rnd() * 3 ) + 2 &#039; gets 2, 3, or 4<BR>Loop While indexSlot2 = indexSlot3<BR><BR>%&#062;<BR><BR>And that does it, I think.<BR><BR>BUT...<BR><BR>But is this *really* what you want? The problem with this is that you always end up with scenario #1 being the first one. Would you prefer that it be scrambled into various positions?<BR><BR>

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

&#060;%<BR>Dim slots(3)<BR>Randomize<BR>slots(1) = Int( Rnd() * 4 ) + 1<BR>Do<BR>&nbsp; &nbsp; slots(2) = Ind( Rnd() * 4 ) + 1<BR>Loop Until slots(2) &#060;&#062; slots(1)<BR>Do<BR>&nbsp; &nbsp; slots(3) = Ind( Rnd() * 4 ) + 1<BR>Loop Until ( slots(3) &#060;&#062; slots(2) AND slots(3) &#060;&#062; slots(1) )<BR>If slots(1) &#060;&#062; 1 AND slots(2) &#060;&#062; 1 AND slots(3) &#060;&#062; 1 Then<BR>&nbsp; &nbsp; slots( Int( Rnd() * 3 ) + 1 ) = 1<BR>End If<BR>%&#062;<BR><BR>See that? I used an array of slots to make the last step easier.<BR><BR>-- I pick any number from 1 to 4 for slot(1).<BR>-- I pick any number from 1 to 4 for slot(2) so long as it doesn&#039;t match slot(1).<BR>-- I pick any number from 1 to 4 for slot(3) so long as it doesn&#039;t match slot(1) or slot(2).<BR>-- If none of the 3 slots contains a 1 for the scenario number, then I randomly pick one of the 3 slots and change its scenario number to 1.<BR><BR>So now you have scenario 1 always picked, but it will be in some random slot, not always slot 1.<BR><BR>I dunno if this is what you wanted, but I thought it would be fun to see what it would take to do it.<BR><BR><BR><BR>

4. Kentium Guest

## look at this

this does exactly what I want-<BR><BR>DIM IndexSlot1,IndexSlot2,IndexSlot3<BR>Randomize &#039; Initialize random-number generator.<BR>IndexSlot1=4<BR>IndexSlot2=Int((3 * Rnd) + 1)<BR>IndexSlot3=Int((3 * Rnd) + 1)<BR> do while IndexSlot3 = IndexSlot2<BR> IndexSlot3=Int((3 * Rnd) + 1)<BR> loop<BR><BR>Function Shuffle( inArray, needed )<BR> incnt = UBound( inArray )<BR> dim outArray<BR> redim outArray( needed )<BR> For i = 1 To needed<BR> choose = Int( incnt * Rnd(1) ) + 1<BR> outArray( i ) = inArray( choose )<BR> inArray( choose ) = inArray( incnt )<BR> incnt = incnt - 1<BR> Next<BR> Shuffle = outArray<BR>End Function<BR><BR>Randomize<BR>dim sceneindex, incnt, i, choose, needed, scene<BR>sceneindex = Array(0,IndexSlot1,IndexSlot2,IndexSlot3)<BR>scene = Shuffle( sceneindex, 3 )<BR><BR>I moved the required Index to position 4, thus my random number generator 1-3 doesn&#039;t ever have to *not* pick 1.<BR><BR>thanks!

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

## Works...but a *LOT* of code...

...compared to my simpler form.<BR><BR>First you have to pick the numbers, then you have to shuffle them. For something as simple as this, with only 3 possible slots, I think the if-I-didn&#039;t-choose-the-required-one-swap-it-for-a-random-slot scheme works better. Three lines of code (which could be 1 if you didn&#039;t mind a long line) in place of all that FUNCTION SHUFFLE code. The picking of the slots to begin with is identical, so it&#039;s only the SHUFFLE that you are replacing with simpler code for this specific instance.<BR><BR>(Except take a look a DO...LOOP WHILE in place of DO WHILE...LOOP. It&#039;s not a big deal in this case, but it&#039;s a trick to remember for more complicated scenarios. You avoid the need to duplicate the code inside the loop prior to the loop.)<BR><BR>Anyway, whatever works. You know me: I&#039;m anal retentive about compact and fast code, all too often when there&#039;s no reason to be.<BR><BR>

6. Kentium Guest

## i tried all that loop stuff....

a couple days ago when I first posted, and you pointed me to the math faq.<BR><BR>I couldn&#039;t sort out the logic with the while/loops....and I kept locking up the server with endless looping.<BR><BR>Your solution is of course better, but I&#039;m glad I went through the motions of figuring out the shuffle function you wrote.<BR><BR>Thanks!

#### Posting Permissions

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