Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random selection from a set 3

Status
Not open for further replies.

Stevo911

Technical User
Apr 26, 2005
33
0
0
ZA
Hi there

An abbreviated version of my problem is that i have a set of 4 numbers for example (1, 2, 3 and 4) and i would like to be able to have an object which holds these 4 numbers where any one of them can be randomly chosen. If, lets say, 2 is randomly chosen, then i would like that the number 2 be excluded from the set (object) and another random selection occur between the numbers 1, 3 and 4. This needs to continue until only one number is left in the set, thus producing a sequence 2,3,4,1 for example.

Can anyone please provide some information on how to create such an object in VBA and how to perform the random selection so that the latest selction is excluded so that the set gets smaller and smaller until there is only one integer left.

Any help or direction is much appreciated.
Thanks
Steve
 
Create 2 arrays, one to hold your "set of 4 numbers" and one to hold the "sequence". See this to generate random numbers ( Randbetween(lowerlimit,upperlimit) ):

1) Generate a random integer between the start and end of "set of 4 numbers" array.
2) Use a random integer as the index number for the array
3) Copy the value to the "sequence" array.
4) Move all the "set of 4 numbers" values below the one selected up one in the array. For example if the array was called aSetofNum() and the random number was 4:

aSetofNum(4)=aSetofNum(5)
aSetofNum(5)=aSetofNum(6)
aSetofNum(6)=aSetofNum(7) ...

5) Reduce the Randbetween upper limit by one.
6) Loop
 
How about....
Code:
Sub Main()
    Dim NumberSet() As Boolean
    Lower = 1
    Upper = 4
    ReDim NumberSet(Upper)
    
    For NumberInSet = Lower To Upper
        
        Do
            Randomize
            RandomNumber = Int((Upper - Lower + 1) * Rnd + Lower)
        Loop Until Not NumberSet(RandomNumber)
        
        NumberSet(RandomNumber) = True
        RandomSeq = RandomSeq & RandomNumber
    Next
    msgbox RandomSeq
End Sub


[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
how to perform the random selection so that the latest selction is excluded so that the set gets smaller and smaller until there is only one integer left.
Sorry didn't read the entire OP.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Or an alternate to CBA's method:

create two arrays, one with your "set of 4 numbers", one nul.

1. generate a random number between 1 to 4.
2 check to see if the value in that index spot of the "set of 4" matchs any value in the second array.
3. If yes, goto 1, else write that value to the first open spot in array 2.
4. repeat until array 2 is full.
 
This is more-or-less CBasicAsslember's solution, but made more efficient by eliminating the requirement to move all the numbers along after each pick
Code:
[blue]
    Dim Pack() As Long
    Dim lp As Long
    Dim Pick As Long
    
    'Set up se
    ReDim Pack(1 To 4) As Long
    For lp = LBound(Pack) To UBound(Pack)
        Pack(lp) = lp
    Next

    Do
        Pick = Int(LBound(Pack) + (Rnd * (UBound(Pack) - LBound(Pack) + 1)))
        MsgBox Pack(Pick)
        Pack(Pick) = Pack(UBound(Pack))
        ReDim Preserve Pack(LBound(Pack) To UBound(Pack) - 1)
    Loop Until UBound(Pack) = LBound(Pack)
    ' One item left in array when we get here[/blue]
 
@strongm, I like it have a star.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
This is more-or-less CBasicAsslember's solution, but made more efficient by eliminating the requirement to move all the numbers along after each pick

Nice job strongm. I miss read and thought Stevo911 wanted to know the sequence as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top