?
Solved

randonly program that will create for me a set of choices

Posted on 2005-03-17
11
Medium Priority
?
210 Views
Last Modified: 2010-04-17
HI all :)

I have a nice question, I have 4 groups where each group contains 7 or 8 elements on. what i try to do is settup a set of choices taking for each group an amount of times where repeat it can be allowed only upto certainly level. this is what i really try to do:

Group 1                                   Group 2                                Group 3                                   Group 4
1.WASH 30                              1.UNC 60                            1.DK   55                                 1.LLNS   65
2.GT      35                              2.CNN 40                            2.KT   40                                 2.OK ST 55
3.ST      25                              3.KS   30                            3.OKL  15                                 3.ZN      40
4.WK     55                              4.FL    30                            4.CUS  35                                4.BAMA  20
5.PITT   30                              5.NV   25                            5.MICH 25                                5.TX      10
6.WST   15                              6.IWST 10                          6.CIN   10                                6.UHST   5
7.CLA     10                             7.NCST  5                           7.IW     5                                 7.STM    5
                                                                                        8.UTF  15

where the number next to the possibility determines the amounts of times can be used, for instance i can have this first convination:
1.WASH,UNC,DK,LLNS  where i will be left off with 29 possibilities for WASH, 59 for UNC, 54 for DK and 64 for LLNS and so
2.
3.
4.
5.
i nedd 200 posible convination having groups interrelate meaning that each convination have to have a menber from each group to be valid; they can not have for instance WASH and CLA for a possible convination due to they are listed in the same group.
I would like to have this in Visual Basic
Thanks
JSCTECHY :)

0
Comment
Question by:jsctechy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 13569162
For each of the lists:

1) make sure at least one entry has a non-zero repeat count
2) generate random idx, 0<=idx<ListLength, until list(idx,repeatcount)>0
3) use the idx'th entry decrement that repeat count
0
 
LVL 22

Expert Comment

by:cookre
ID: 13569188
Any quick way to determine if a given list has at least one entry with a  non-zero repeat count is to increment a list specific GoneToZero counter.  Each time a list entry's repeat counter is decremented from 1 to zero, increment that list's GoneToZero counter.  As long as a lists GoneToZero counter is less than the list length, you know at least one non-zero repeat counter remains in that list.
0
 
LVL 1

Author Comment

by:jsctechy
ID: 13569353
ok but how should i start?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 22

Expert Comment

by:cookre
ID: 13569956
That's an implementation question that depends on which language and compiler you use.  The ideal approach would be three dimensioned array that contains the groups, lists, and repeat counts, and that means both strings and numerics in the same array.

If you can't mix types in a single array, either explicitly, or implicitly with an array of structures, you'll have to keep several arrays.
0
 
LVL 22

Assisted Solution

by:JesterToo
JesterToo earned 2000 total points
ID: 13570533
Here is a VBScript that solves this problem... you could easily change a line or two to compile it in VB if you need... or just execute it like this to write 200 lines of output to a text file:  

   cscript  /nologo RandomCombos.vbs  >  <YourFileName>


'RandomCombos.vbs
'
Dim arrGroup1(6), arrLimit1(6)
Dim arrGroup2(6), arrLimit2(6)
Dim arrGroup3(7), arrLimit3(7)
Dim arrGroup4(6), arrLimit4(6)
Dim Combos
Dim n

Combos = 200

arrGroup1(0) = "WASH"  : arrLimit1(0) = 30
arrGroup1(1) = "GT"    : arrLimit1(1) = 35
arrGroup1(2) = "ST"    : arrLimit1(2) = 25
arrGroup1(3) = "WK"    : arrLimit1(3) = 55
arrGroup1(4) = "PITT"  : arrLimit1(4) = 30
arrGroup1(5) = "WST"   : arrLimit1(5) = 15
arrGroup1(6) = "CLA"   : arrLimit1(6) = 10
'                                      ===
'              sum of Group1 limits =  200

arrGroup2(0) = "UNC"   : arrLimit2(0) = 60
arrGroup2(1) = "CNN"   : arrLimit2(1) = 40
arrGroup2(2) = "KS"    : arrLimit2(2) = 30
arrGroup2(3) = "FL"    : arrLimit2(3) = 30
arrGroup2(4) = "NV"    : arrLimit2(4) = 25
arrGroup2(5) = "IWST"  : arrLimit2(5) = 10
arrGroup2(6) = "NCST"  : arrLimit2(6) =  5
'                                      ===
'              sum of Group2 limits =  200

arrGroup3(0) = "DK"    : arrLimit3(0) = 55
arrGroup3(1) = "KT"    : arrLimit3(1) = 40
arrGroup3(2) = "OKL"   : arrLimit3(2) = 15
arrGroup3(3) = "CUS"   : arrLimit3(3) = 35
arrGroup3(4) = "MICH"  : arrLimit3(4) = 25
arrGroup3(5) = "CIN"   : arrLimit3(5) = 10
arrGroup3(6) = "IW"    : arrLimit3(6) =  5
arrGroup3(7) = "UTF"   : arrLimit3(7) = 15
'                                      ===
'              sum of Group3 limits =  200

arrGroup4(0) = "LLNS"  : arrLimit4(0) = 65
arrGroup4(1) = "OK ST" : arrLimit4(1) = 55
arrGroup4(2) = "ZN"    : arrLimit4(2) = 40
arrGroup4(3) = "BAMA"  : arrLimit4(3) = 20
arrGroup4(4) = "TX"    : arrLimit4(4) = 10
arrGroup4(5) = "UHST"  : arrLimit4(5) =  5
arrGroup4(6) = "STM"   : arrLimit4(6) =  5
'                                      ===
'              sum of Group4 limits =  200

Randomize

For n = 1 to 200
   strLine = ""

   ' choose an eligible entry from Group1
   Do While True
      x = Int(7 * Rnd)
      If arrLimit1(x) > 0 Then
         strLine = strLine & arrGroup1(x)
         arrLimit1(x) = arrLimit1(x) - 1
         Exit Do
      End If
   Loop

   ' choose an eligible entry from Group2
   Do While True
      x = Int(7 * Rnd)
      If arrLimit2(x) > 0 Then
         strLine = strLine & ", " & arrGroup2(x)
         arrLimit2(x) = arrLimit2(x) - 1
         Exit Do
      End If
   Loop

   ' choose an eligible entry from Group3
   Do While True
      x = Int(8 * Rnd)
      If arrLimit3(x) > 0 Then
         strLine = strLine & ", " & arrGroup3(x)
         arrLimit3(x) = arrLimit3(x) - 1
         Exit Do
      End If
   Loop

   ' choose an eligible entry from Group4
   Do While True
      x = Int(7 * Rnd)
      If arrLimit4(x) > 0 Then
         strLine = strLine & ", " & arrGroup4(x)
         arrLimit4(x) = arrLimit4(x) - 1
         Exit Do
      End If
   Loop

   Wscript.Echo n & ". " & strLine
Next

HTH,
Lynn
0
 
LVL 1

Author Comment

by:jsctechy
ID: 13577435
hi Lynn:

If i'm to put this code in Microsoft Visual Studio. NET should i create a new project, but there are a few option which one should i pick? i have Project Types and in from of i have templates which one should i use or how can i compile this code to see the output?
Thanks

jsctechy
0
 
LVL 1

Author Comment

by:jsctechy
ID: 13578268
i got it to run but now the out put show me alot of dups any idea?
here is the out put:
1. WASH, FL, MICH, LLNS
2. GT, FL, DK, STM
3. ST, KS, IW, STM
4. PITT, NCST, OKL, ZN
5. WST, KS, MICH, ZN
6. WASH, NV, MICH, ZN
7. GT, KS, KT, UHST
8. ST, FL, CUS, OK ST
9. ST, NV, MICH, OK ST
10. GT, NCST, MICH, UHST
11. ST, KS, CUS, BAMA
12. ST, CNN, IW, LLNS
13. ST, NCST, OKL, OK ST
14. GT, IWST, MICH, STM
15. WK, NV, CUS, BAMA
16. GT, FL, IW, ZN
17. ST, CNN, UTF, OK ST
18. WST, UNC, DK, LLNS
19. ST, NCST, MICH, STM
20. CLA, KS, OKL, UHST
21. GT, NV, IW, LLNS
22. WST, NCST, DK, ZN
23. CLA, UNC, MICH, ZN
24. WST, KS, OKL, ZN
25. WST, KS, KT, TX
26. ST, NV, OKL, BAMA
27. WST, IWST, KT, OK ST
28. WK, UNC, DK, STM
29. GT, NV, UTF, TX
30. GT, CNN, UTF, UHST
31. CLA, NV, DK, UHST
32. WST, UNC, MICH, OK ST
33. PITT, UNC, IW, BAMA
34. GT, FL, OKL, LLNS
35. CLA, CNN, OKL, TX
36. PITT, NV, UTF, OK ST
37. ST, NV, CUS, OK ST
38. PITT, KS, MICH, OK ST
39. GT, KS, DK, OK ST
40. GT, NV, DK, TX
41. WST, KS, OKL, TX
42. CLA, CNN, OKL, BAMA
43. WK, FL, KT, ZN
44. GT, UNC, OKL, ZN
45. WASH, IWST, OKL, BAMA
46. WASH, NV, OKL, ZN
47. WK, UNC, MICH, ZN
48. WK, IWST, CUS, LLNS
49. WST, NV, MICH, ZN
50. PITT, CNN, MICH, TX
51. ST, NV, CIN, OK ST
52. ST, UNC, UTF, OK ST
53. GT, FL, DK, OK ST
54. PITT, NV, OKL, OK ST
55. CLA, NV, CIN, TX
56. PITT, IWST, CUS, TX
57. WK, NV, KT, TX
58. WST, KS, CUS, TX
59. PITT, KS, KT, ZN
60. GT, CNN, CUS, OK ST
61. WASH, UNC, MICH, LLNS
62. WASH, KS, KT, BAMA
63. GT, CNN, CUS, ZN
64. WASH, IWST, UTF, BAMA
65. ST, CNN, CIN, OK ST
66. WK, NV, MICH, ZN
67. WASH, NV, CIN, LLNS
68. PITT, IWST, OKL, BAMA
69. WST, UNC, CIN, BAMA
70. WST, IWST, UTF, ZN
71. GT, NV, CIN, OK ST
72. WK, CNN, DK, BAMA
73. GT, FL, CIN, OK ST
74. GT, UNC, KT, LLNS
75. WST, KS, UTF, ZN
76. WASH, CNN, KT, OK ST
77. WASH, CNN, MICH, LLNS
78. ST, FL, DK, OK ST
79. WST, KS, DK, OK ST
80. PITT, IWST, KT, BAMA
81. ST, UNC, CUS, LLNS
82. PITT, FL, UTF, ZN
83. CLA, UNC, MICH, BAMA
84. WASH, UNC, DK, LLNS
85. CLA, CNN, CUS, OK ST
86. WST, FL, OKL, BAMA
87. GT, FL, CIN, OK ST
88. WK, FL, DK, OK ST
89. PITT, FL, MICH, OK ST
90. ST, KS, MICH, BAMA
91. PITT, CNN, UTF, BAMA
92. WK, IWST, CIN, ZN
93. CLA, FL, DK, OK ST
94. GT, NV, MICH, ZN
95. CLA, KS, CIN, BAMA
96. WASH, KS, DK, ZN
97. PITT, KS, MICH, LLNS
98. WASH, UNC, DK, LLNS
99. ST, UNC, CUS, BAMA
100. GT, NV, UTF, OK ST
101. GT, FL, UTF, ZN
102. ST, UNC, MICH, LLNS
103. PITT, NV, DK, BAMA
104. ST, CNN, MICH, ZN
105. GT, FL, KT, BAMA
106. ST, UNC, CUS, ZN
107. GT, UNC, CUS, ZN
108. PITT, CNN, KT, OK ST
109. ST, KS, DK, ZN
110. PITT, CNN, DK, ZN
111. WASH, CNN, CUS, LLNS
112. PITT, KS, CUS, LLNS
113. PITT, UNC, KT, OK ST
114. WK, CNN, MICH, OK ST
115. WASH, UNC, KT, ZN
116. WK, KS, MICH, ZN
117. PITT, UNC, DK, ZN
118. WK, UNC, CUS, ZN
119. GT, UNC, DK, LLNS
120. WASH, FL, DK, OK ST
121. PITT, CNN, DK, OK ST
122. ST, UNC, DK, LLNS
123. ST, UNC, KT, OK ST
124. GT, FL, UTF, OK ST
125. WASH, KS, CUS, LLNS
126. WASH, CNN, DK, LLNS
127. WASH, UNC, DK, OK ST
128. PITT, NV, KT, ZN
129. WASH, FL, KT, OK ST
130. ST, CNN, CUS, LLNS
131. ST, CNN, CUS, LLNS
132. PITT, CNN, KT, OK ST
133. WASH, CNN, KT, ZN
134. PITT, NV, KT, OK ST
135. GT, NV, UTF, ZN
136. WASH, CNN, DK, ZN
137. GT, CNN, UTF, OK ST
138. GT, KS, DK, LLNS
139. WASH, FL, CUS, LLNS
140. GT, KS, KT, OK ST
141. WASH, FL, DK, ZN
142. WASH, CNN, DK, ZN
143. WK, KS, KT, LLNS
144. PITT, UNC, CUS, ZN
145. WK, CNN, CUS, ZN
146. WK, KS, DK, LLNS
147. GT, FL, CUS, ZN
148. WK, UNC, DK, OK ST
149. GT, CNN, KT, OK ST
150. WASH, FL, DK, OK ST
151. PITT, FL, KT, LLNS
152. GT, CNN, CUS, LLNS
153. GT, CNN, CUS, OK ST
154. GT, UNC, KT, OK ST
155. PITT, CNN, DK, OK ST
156. PITT, CNN, DK, OK ST
157. PITT, FL, CUS, LLNS
158. WASH, KS, CUS, OK ST
159. PITT, UNC, KT, LLNS
160. WK, FL, DK, OK ST
161. WK, FL, DK, OK ST
162. WASH, FL, KT, LLNS
163. WASH, CNN, KT, LLNS
164. WASH, FL, CUS, LLNS
165. WK, KS, CUS, OK ST
166. WK, KS, DK, OK ST
167. WK, CNN, CUS, OK ST
168. WK, CNN, KT, OK ST
169. WK, CNN, KT, OK ST
170. WK, CNN, DK, LLNS
171. WK, CNN, CUS, LLNS
172. WK, UNC, KT, LLNS
173. WK, UNC, KT, LLNS
174. WK, UNC, CUS, LLNS
175. WK, UNC, DK, LLNS
176. WK, UNC, KT, LLNS
177. WK, UNC, CUS, LLNS
178. WK, UNC, CUS, LLNS
179. WK, UNC, KT, LLNS
180. WK, UNC, KT, LLNS
181. WK, UNC, DK, LLNS
182. WK, UNC, KT, LLNS
183. WK, UNC, DK, LLNS
184. WK, UNC, DK, LLNS
185. WK, UNC, KT, LLNS
186. WK, UNC, KT, LLNS
187. WK, UNC, KT, LLNS
188. WK, UNC, KT, LLNS
189. WK, UNC, DK, LLNS
190. WK, UNC, DK, LLNS
191. WK, UNC, DK, LLNS
192. WK, UNC, DK, LLNS
193. WK, UNC, DK, LLNS
194. WK, UNC, DK, LLNS
195. WK, UNC, DK, LLNS
196. WK, UNC, DK, LLNS
197. WK, UNC, DK, LLNS
198. WK, UNC, DK, LLNS
199. WK, UNC, DK, LLNS
200. WK, UNC, DK, LLNS

see what i told you :)
0
 
LVL 22

Expert Comment

by:JesterToo
ID: 13578324
I'd use the "Console Application" project since that's most similar to the way this VBScript works... but you could use "Windows App" if you prefer.  Just view the output file with Notepad or something similar.  However, unless you're making this code part of a larger program there's no benefit in converting it to VB6 or .Net... it runs in under 2 seconds just as it it.

-- Lynn
0
 
LVL 22

Expert Comment

by:JesterToo
ID: 13578401
>>>see what i told you :)<<<

Please recheck your question... there is NO mention of UNIQUE COMBINATIONS.  The code I provided conforms exactly to your stated specifications.

There is probably a fairly easy way to avoid duplicate combinations... however that will make the algorithm a bit more complex.  I'll see if I can work that in.

-- Lynn
0
 
LVL 22

Expert Comment

by:cookre
ID: 13578678
When a new combination is posited, just check to see if it's already been generated.  If so, skip that one (without decrementing the repeat counts).  At the same time maintain a ConsecutiveHits counter that's cleared with each accepted combination.  If the ConsecutiveHits counter gets above a certain value, quit.

0
 
LVL 22

Accepted Solution

by:
JesterToo earned 2000 total points
ID: 13578988
Hello, cookre... seems we think much alike :-)

Here is a new version which eliminates the duplicates.  I've arbitrarily set a threshold of 1000 consecutive dupes generated.  Due to the pseudo-randomness of how the combinations are generated and the constraints placed on how many times any given group entry can be used you will likely never gnerate the full 200 possible combinations... the highest number I could get was 188 after running it for 20-30 times.  Here's the new version:

Dim arrGroup1(6), arrLimit1(6)
Dim arrGroup2(6), arrLimit2(6)
Dim arrGroup3(7), arrLimit3(7)
Dim arrGroup4(6), arrLimit4(6)
Dim Combos(199)
Dim ThisCombo
Dim Grp1, Grp2, Grp3, Grp4
Dim x, UniqueCombosFound
Dim ConsecutiveDupes
Dim ConsecutiveDupes2
Dim cd

arrGroup1(0) = "WASH"  : arrLimit1(0) = 30
arrGroup1(1) = "GT"    : arrLimit1(1) = 35
arrGroup1(2) = "ST"    : arrLimit1(2) = 25
arrGroup1(3) = "WK"    : arrLimit1(3) = 55
arrGroup1(4) = "PITT"  : arrLimit1(4) = 30
arrGroup1(5) = "WST"   : arrLimit1(5) = 15
arrGroup1(6) = "CLA"   : arrLimit1(6) = 10
'                                      ===
'              sum of Group1 limits =  200

arrGroup2(0) = "UNC"   : arrLimit2(0) = 60
arrGroup2(1) = "CNN"   : arrLimit2(1) = 40
arrGroup2(2) = "KS"    : arrLimit2(2) = 30
arrGroup2(3) = "FL"    : arrLimit2(3) = 30
arrGroup2(4) = "NV"    : arrLimit2(4) = 25
arrGroup2(5) = "IWST"  : arrLimit2(5) = 10
arrGroup2(6) = "NCST"  : arrLimit2(6) =  5
'                                      ===
'              sum of Group2 limits =  200

arrGroup3(0) = "DK"    : arrLimit3(0) = 55
arrGroup3(1) = "KT"    : arrLimit3(1) = 40
arrGroup3(2) = "OKL"   : arrLimit3(2) = 15
arrGroup3(3) = "CUS"   : arrLimit3(3) = 35
arrGroup3(4) = "MICH"  : arrLimit3(4) = 25
arrGroup3(5) = "CIN"   : arrLimit3(5) = 10
arrGroup3(6) = "IW"    : arrLimit3(6) =  5
arrGroup3(7) = "UTF"   : arrLimit3(7) = 15
'                                      ===
'              sum of Group3 limits =  200

arrGroup4(0) = "LLNS"  : arrLimit4(0) = 65
arrGroup4(1) = "OK ST" : arrLimit4(1) = 55
arrGroup4(2) = "ZN"    : arrLimit4(2) = 40
arrGroup4(3) = "BAMA"  : arrLimit4(3) = 20
arrGroup4(4) = "TX"    : arrLimit4(4) = 10
arrGroup4(5) = "UHST"  : arrLimit4(5) =  5
arrGroup4(6) = "STM"   : arrLimit4(6) =  5
'                                      ===
'              sum of Group4 limits =  200

Randomize
UniqueCombosFound = 0
ConsecutiveDupes  = 0
ConsecutiveDupes2 = 0

Do Until UniqueCombosFound = 200
   Grp1 = -1
   Grp2 = -1
   Grp3 = -1
   Grp4 = -1
   ThisCombo = ""

   ' choose an eligible entry from Group1
   Do While True
      x = Int(7 * Rnd)
      If arrLimit1(x) > 0 Then
         ThisCombo = ThisCombo & arrGroup1(x)
         Grp1 = x
         Exit Do
      End If
   Loop

   ' choose an eligible entry from Group2
   Do While True
      x = Int(7 * Rnd)
      If arrLimit2(x) > 0 Then
         ThisCombo = ThisCombo & ", " & arrGroup2(x)
         Grp2 = x
         Exit Do
      End If
   Loop

   ' choose an eligible entry from Group3
   Do While True
      x = Int(8 * Rnd)
      If arrLimit3(x) > 0 Then
         ThisCombo = ThisCombo & ", " & arrGroup3(x)
         Grp3 = x
         Exit Do
      End If
   Loop

   ' choose an eligible entry from Group4
   Do While True
      x = Int(7 * Rnd)
      If arrLimit4(x) > 0 Then
         ThisCombo = ThisCombo & ", " & arrGroup4(x)
         Grp4 = x
         Exit Do
      End If
   Loop

   For x = 0 To UniqueCombosFound
      If Combos(x) = ThisCombo Then
         x = -1
         Exit For
      End If
   Next

   If x <> -1 Then
      Combos(x)         = ThisCombo
      UniqueCombosFound = UniqueCombosFound + 1
      arrLimit1(Grp1)   = arrLimit1(Grp1) - 1
      arrLimit2(Grp2)   = arrLimit2(Grp2) - 1
      arrLimit3(Grp3)   = arrLimit3(Grp3) - 1
      arrLimit4(Grp4)   = arrLimit4(Grp4) - 1
      ConsecutiveDupes  = 0
      ConsecutiveDupes2 = 0
      cd                = 0
      Wscript.Echo UniqueCombosFound & ". " & ThisCombo
   Else
      ConsecutiveDupes  = ConsecutiveDupes + 1
      ConsecutiveDupes2 = ConsecutiveDupes2 + 1
      If ConsecutiveDupes2 = 100 Then
         ConsecutiveDupes2 = 0
         cd = cd + 1
         Wscript.Echo (cd * 100) & " consecutive dupes generated..."
      End If
      If ConsecutiveDupes = 1000 Then
         Wscript.Echo "Terminating due to threshold exceeded."
         Exit Do
      End If
   End If
Loop                                                
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Progress
Introduction to Processes

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question