Results 1 to 10 of 10

Thread: how to make a random array not generate a number it has already generated

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    how to make a random array not generate a number it has already generated

    Hi

    The thing is, im trying to make a random array of three numbers. Now, i've made the random array but i need it to not generate any numbers that it has already generated. for example:

    1 2 3, not 1,1,2 or 1,2,1.

    heres some code i made up to try and do it:

    Code:
    'generates three random numbers
    Randomize
    
    picdisplay1.Cls
    picdisplay2.Cls
    picdisplay3.Cls
    
    lott_num(1) = Int((Rnd * 5) + 1)
    lott_num(2) = Int((Rnd * 5) + 1)
    lott_num(3) = Int((Rnd * 5) + 1)
    
    If lott_num(1) = lott_num(2) Or lott_num(3) Then
     lott_num(1) = Int((Rnd * 5) + 1)
     Else
    If lott_num(2) = lott_num(3) Then
     lott_num(2) = Int((Rnd * 5) + 1)
    End If
    End If
    picdisplay1.Print (lott_num(1))
    picdisplay2.Print (lott_num(2))
    picdisplay3.Print (lott_num(3))
    if this is the right method and its some mistake ive made then i would appreciate it being pointed out, however, if there is a more elegant method, then im open to suggestions.

    Thanks!
    Cal

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: how to make a random array not generate a number it has already generated

    Look at this FAQ topic on random numbers. Post 4 in that FAQ will interest you.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: how to make a random array not generate a number it has already generated

    thanks

  4. #4
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: how to make a random array not generate a number it has already generated

    Cal

    Before tackling your actual question, may I make
    a suggestion regarding use of indenting....

    The following is your original code with "better"
    indenting. It surely helps me follow things, and
    probably will help you and others, too.

    Code:
    'generates three random numbers
    Randomize
    
    picdisplay1.Cls
    picdisplay2.Cls
    picdisplay3.Cls
    
    lott_num(1) = Int((Rnd * 5) + 1)
    lott_num(2) = Int((Rnd * 5) + 1)
    lott_num(3) = Int((Rnd * 5) + 1)
    
    If lott_num(1) = lott_num(2) Or lott_num(3) Then
        lott_num(1) = Int((Rnd * 5) + 1)
    Else
        If lott_num(2) = lott_num(3) Then
            lott_num(2) = Int((Rnd * 5) + 1)
        End If
    End If
    picdisplay1.Print (lott_num(1))
    picdisplay2.Print (lott_num(2))
    picdisplay3.Print (lott_num(3))
    Call me finnicky, but it really does help

    Spoo

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: how to make a random array not generate a number it has already generated

    yeah, i usually do indent well, but im just testing code out quickly so havent bothered to do it this time.
    Appreciate the thaught tho

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: how to make a random array not generate a number it has already generated

    Cal

    Now, on to your actual question.

    The problem with your current logic is that
    you still leave yourself open to a "repeat"

    Take your first branch ..

    Code:
    If lott_num(1) = lott_num(2) Or lott_num(3) Then
        lott_num(1) = Int((Rnd * 5) + 1)
    Theoretically, lott_num(1) could be a repeat and you'd
    have no way to deal with it.

    Seems to me that you need a loop. Essentially, you would
    stay in the loop until you assure yourself that you do not
    have a repeat, and at that point, you'd exit the loop.

    Spoo

  7. #7
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: how to make a random array not generate a number it has already generated

    Quote Originally Posted by calarexar View Post
    yeah, i usually do indent well, but im just testing code out quickly so havent bothered to do it this time.
    Appreciate the thaught tho
    Haha.. ok, just checking.

    Spoo

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: how to make a random array not generate a number it has already generated

    thanks, il try using a boolean or something.

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: how to make a random array not generate a number it has already generated

    Spoo, hows this?

    Code:
    Do
    If lott_num(1) = lott_num(2) Or lott_num(3) Then
        found = True
        lott_num(1) = Int((Rnd * 5) + 1)
    Else
        If lott_num(2) = lott_num(3) Then
            found = True
            lott_num(2) = Int((Rnd * 5) + 1)
    Loop Until found = False
    End If
    End If
    See! i do indent!

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    36

    Re: how to make a random array not generate a number it has already generated

    woops...just seen wats wrong wit dat...dear god im a noob at this if there ever was one!

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