Results 1 to 10 of 10

Thread: A simple Do While Loop gone bad....

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323

    A simple Do While Loop gone bad....

    I am doing a loop and it's giving me a hard time. The result of this loop is the opposite of what I'm looking for. Here's the code:
    Code:
    <%
    Dim Suit
    Dim Value
    Dim numValue
    Dim intScore
    
    intScore = 0
    
    Sub GetCards
    For x = 0 To 4
    If Request.Form("Session" & x) <> "on" Then
    			randomize()
    		'Do Until (randomNumber <> Session0) AND (randomNumber <> Session1) AND (randomNumber <> Session2) AND (randomNumber <> Session3) AND (randomNumber <> Session4)
    			randomNumber=Int(51 * rnd())
    		'Loop
    	Session(x) = randomNumber
    		Loop
    End If
    %>
    I had to comment the Do While Loop out because it makes every card the same one. I have tried a lot of different ways to make it work but it never will. The goal is to make it keep generating random numbers until it's chosen a card that has not been chosen already.

    Please tell me where I'm going wrong with this. Thank you.

    If you know a better way to get this to work please tell me.
    If you think education is expensive, try ignorance.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    I am using 5 sessions to keep track of the cards being used.

    Session0
    Session1
    Session2
    Session3
    Session4

    I tried different ways of working with it including an array but nothing would make it work right.

    Thanks for your help.
    If you think education is expensive, try ignorance.

  3. #3
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    Ok, a couple more questions then:

    Where is randomNumber declared?

    Is Session0 a variable that's declared elsewhere?

    Is this the complete code that giving you the problem or is there more?


    Chris
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    This is the complete code that is giving me the problems. I have a lot more code to make it look nice and format it correctly, but that part is working fine.

    The goal I'm trying to reach is to make sure the different sessions do not equal each other (different cards). Even though they are not dimensioned anywhere that isn't where I'm having the problem. It is using each variable, but it's sometime using the same one as one of the other sessions.
    If you think education is expensive, try ignorance.

  5. #5
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    To access a Session value you can't refer to it as Session0. You must have previous set the value:

    Session("card1") = myNumber

    Session(1) or Session("card1") would then refer to the value of the variable myNumber.

    Also, the first time through the loop randomNumber isn't set to anything. You could do this:

    randomNumber=Int(51 * rnd())

    Do Until (randomNumber <> Session(1)) AND (randomNumber <> Session(2)) AND (randomNumber <> Session(3)) AND (randomNumber <> Session(4)) AND (randomNumber <> Session(5))

    randomNumber=Int(51 * rnd())


    Loop
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  6. #6
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    One more thing, you are also missing the NEXT for your FOR loop and missing the END SUB.


    Chris
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  7. #7
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    I gotta run, if nobody else helps you can email me the code and I'll take a look at it.


    Chris
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    Here is the zip file which has all of the files being used included (3 files). I am unable to get your email address (Psyrus) so I just uploaded it here.

    Anyone wanting to help is more than welcome to view these files. Thanks for any help.
    If you think education is expensive, try ignorance.

  9. #9
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    Hey,

    Try changing your loop to this:

    Do Until (randomNumber <> Session(0) _
    AND (randomNumber <> Session(1)) _
    AND (randomNumber <> Session(2)) _
    AND (randomNumber <> Session(3)) _
    AND (randomNumber <> Session(4))

    randomNumber=Int(51 * rnd())

    Loop

    In your version you where comparing the randomNumber to Session0 which from what I have read in your code can only be a text value.
    You have to check randomNumber against the Session value.
    I ran it and it worked fine.
    One other thing you will have to address, unless it doesn't matter, is the fact that you can still get the same card back after you have chosen to discard it.
    You might consider adding an array to store the used numbers so they do not appear in any subsequent selections during that particular hand.

    Chris
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    Pefect, thank you.

    I will look into the array also, I'm sure I shouldn't have too many problems with that but I will post a reply if I do.

    Thanks again for your help (everyone).
    If you think education is expensive, try ignorance.

Posting Permissions

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



Click Here to Expand Forum to Full Width