
Fun with random numbers
I'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'm using this code<BR><BR>DIM IndexSlot1,IndexSlot2,IndexSlot3<BR>Randomize ' Initialize randomnumber 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 <> IndexSlot2 then<BR> exit do<BR> end if<BR> loop<BR><BR>I'm trying to make it so that index 2 and 3 don't equal the required index, or each other.<BR><BR>I think i'm close...but for some reason I still get index 2 and 3 to come up the same on occasion.<BR><BR>Ideas?

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're trying to make this simpler, but you've got a pair of goofs.<BR><BR>Comments preceded by ###<BR><BR><%<BR>DIM IndexSlot1,IndexSlot2,IndexSlot3<BR>Randomize ' Initialize randomnumber 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) ' original<BR><BR>### If you don't want to pick 1, then why have your low number<BR>### range start with 1???<BR>IndexSlot2=Int((4 * Rnd) + 1)<BR>### don'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> IndexSlot2=Int((4 * Rnd) + 1)<BR>loop<BR>### same comment about IndexSlot3... why start at 1 if you don'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'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> <BR> IndexSlot3=Int((4 * Rnd) + 1)<BR> if IndexSlot3 <> IndexSlot2 then<BR> exit do<BR> end if<BR>loop<BR>%><BR><BR>********************* ******<BR><BR>So, let's start over:<BR><BR><%<BR>Dim indexSlot1, indexSlot2, indexSlot3<BR>Randomize<BR><BR>' you *must* have a 1 you said, so why not just assign it:<BR>indexSlot1 = 1<BR><BR>' now don't include the value 1 in the possible values picked<BR>' for slots 2 and 3!<BR>indexSlot2 = Int( Rnd() * 3 ) + 2 ' gets 2, 3, or 4<BR><BR>' we shorten the code a little using DO...LOOP WHILE instead<BR>' of using DO WHILE...LOOP:<BR><BR>Do <BR> indexSlot3 = Int( Rnd() * 3 ) + 2 ' gets 2, 3, or 4<BR>Loop While indexSlot2 = indexSlot3<BR><BR>%><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>

How about this?
<%<BR>Dim slots(3)<BR>Randomize<BR>slots(1) = Int( Rnd() * 4 ) + 1<BR>Do<BR> slots(2) = Ind( Rnd() * 4 ) + 1<BR>Loop Until slots(2) <> slots(1)<BR>Do<BR> slots(3) = Ind( Rnd() * 4 ) + 1<BR>Loop Until ( slots(3) <> slots(2) AND slots(3) <> slots(1) )<BR>If slots(1) <> 1 AND slots(2) <> 1 AND slots(3) <> 1 Then<BR> slots( Int( Rnd() * 3 ) + 1 ) = 1<BR>End If<BR>%><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't match slot(1).<BR> I pick any number from 1 to 4 for slot(3) so long as it doesn'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>

look at this
this does exactly what I want<BR><BR>DIM IndexSlot1,IndexSlot2,IndexSlot3<BR>Randomize ' Initialize randomnumber 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 13 doesn't ever have to *not* pick 1.<BR><BR>thanks!

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 ifIdidn'tchoosetherequiredoneswapitforarandomslot scheme works better. Three lines of code (which could be 1 if you didn'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'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's not a big deal in this case, but it'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'm anal retentive about compact and fast code, all too often when there's no reason to be.<BR><BR>

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'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'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

Forum Rules

