Results 1 to 3 of 3

Thread: Random Order

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Question Random Order

    I am making this game where I need to put 26 values in a random order... The problem is that I don't know how to check if the random number generated has already been generated before or not... Even a bigger problem, I don't know how to make the loop so that when all the values are assigned, the do loop is exited...
    Any clue on how this could be done...
    Hey... If you found this post helpful please rate it.

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Random Order


  3. #3
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Random Order

    Use this function:

    VB Code:
    1. Public Function intRandomBetween(lowerNum As Integer, higherNum As Integer) As Integer
    2.  
    3.     intRandomBetween = Int((higherNum - lowerNum + 1) * Rnd + lowerNum)
    4.  
    5. End Function


    With this code:


    VB Code:
    1. Dim i, rndm As Integer
    2. Dim gend, nms As String: gend = "": nms = ""
    3.  
    4. Do While i < 26
    5.     rndm = intRandomBetween(1, 26)
    6.         If InStr(gend, "(" & rndm & ")") <= 0 Then
    7.             nms = nms & rndm & ", "
    8.             i = i + 1: gend = gend & "(" & rndm & ")"
    9.         End If
    10. Loop
    11.  
    12. Label1.Caption = Mid$(nms, 1, Len(nms) - 2)

    If you put that in a command button, or any object's click event or something of the sort, it'll set a caption to numbers 1 through 26 generated in random order for you, separated by commas.

    Obviously, you could modify this accordingly to suit your needs and assign values in whatever order the numbers are generated, of course.
    Last edited by BrendanDavis; Jan 11th, 2007 at 01:32 PM.

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