|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Eliminate duplication in an arrayUsing VB6 I have created an array using numbers (from 0 - 24). I want to
randomize the numbers to fill the array but if a duplicate number is generated by the random function, disregard that number and go on to the next until I have 10 unique numbers. Any help would be greately appreciated. BobbyS wrote:
> Using VB6 I have created an array using numbers (from 0 - 24). I want Has the teacher suggested any potential methods to solve this?> to randomize the numbers to fill the array but if a duplicate number > is generated by the random function, disregard that number and go on > to the next until I have 10 unique numbers. Any help would be > greately appreciated. "Karl E. Peterson" <k***@mvps.org> wrote in message Egad, if I just gave someone the answer to a homework question, kick me.news:OTahVPeJGHA.740@TK2MSFTNGP12.phx.gbl... >> Using VB6 I have created an array using numbers (from 0 - 24). I want >> to randomize the numbers to fill the array but if a duplicate number >> is generated by the random function, disregard that number and go on >> to the next until I have 10 unique numbers. Any help would be >> greately appreciated. > > Has the teacher suggested any potential methods to solve this? Jeff Johnson [MVP: VB] wrote:
> "Karl E. Peterson" <k***@mvps.org> wrote in message <g>> news:OTahVPeJGHA.740@TK2MSFTNGP12.phx.gbl... > >>> Using VB6 I have created an array using numbers (from 0 - 24). I >>> want to randomize the numbers to fill the array but if a duplicate >>> number is generated by the random function, disregard that number >>> and go on to the next until I have 10 unique numbers. Any help >>> would be greately appreciated. >> >> Has the teacher suggested any potential methods to solve this? > > Egad, if I just gave someone the answer to a homework question, kick > me. Karl,
You are safe...I am the teacher...But I was given an English class and I needed some help with this logic to generate random words for spelling test. Got everything else done.. Thanks. Show quoteHide quote "Karl E. Peterson" wrote: > Jeff Johnson [MVP: VB] wrote: > > "Karl E. Peterson" <k***@mvps.org> wrote in message > > news:OTahVPeJGHA.740@TK2MSFTNGP12.phx.gbl... > > > >>> Using VB6 I have created an array using numbers (from 0 - 24). I > >>> want to randomize the numbers to fill the array but if a duplicate > >>> number is generated by the random function, disregard that number > >>> and go on to the next until I have 10 unique numbers. Any help > >>> would be greately appreciated. > >> > >> Has the teacher suggested any potential methods to solve this? > > > > Egad, if I just gave someone the answer to a homework question, kick > > me. > > <g> > -- > Working without a .NET? > http://classicvb.org/ > > > I guess we don't have to kick *Jeff* then. Dammit! <g>
-- Show quoteHide quoteMike Microsoft MVP Visual Basic "BobbyS" <Bob***@discussions.microsoft.com> wrote in message news:19DED5BB-2105-438D-982A-1DB599FBD224@microsoft.com... > Karl, > You are safe...I am the teacher...But I was given an English class and I > needed some help with this logic to generate random words for spelling > test. > Got everything else done.. > > Thanks. > > "Karl E. Peterson" wrote: > >> Jeff Johnson [MVP: VB] wrote: >> > "Karl E. Peterson" <k***@mvps.org> wrote in message >> > news:OTahVPeJGHA.740@TK2MSFTNGP12.phx.gbl... >> > >> >>> Using VB6 I have created an array using numbers (from 0 - 24). I >> >>> want to randomize the numbers to fill the array but if a duplicate >> >>> number is generated by the random function, disregard that number >> >>> and go on to the next until I have 10 unique numbers. Any help >> >>> would be greately appreciated. >> >> >> >> Has the teacher suggested any potential methods to solve this? >> > >> > Egad, if I just gave someone the answer to a homework question, kick >> > me. MikeD wrote:
> I guess we don't have to kick *Jeff* then. Dammit! <g> LOL!"MikeD" <nob***@nowhere.edu> wrote in message PPPHHHHBBBBTTTT!!!!news:u6yx62fJGHA.916@TK2MSFTNGP10.phx.gbl... > I guess we don't have to kick *Jeff* then. Dammit! <g> "BobbyS" <Bob***@discussions.microsoft.com> wrote in message You could create a second array of 25 elements and assign the numbers 0 to news:6D620493-88F3-494B-B710-DF046E4CDA38@microsoft.com... > Using VB6 I have created an array using numbers (from 0 - 24). I want to > randomize the numbers to fill the array but if a duplicate number is > generated by the random function, disregard that number and go on to the > next > until I have 10 unique numbers. Any help would be greately appreciated. 24 to each element. Then, when you generate a random number from 0 to 24, you use it as an index into this array. If that element holds a valid number, you use it and then write -1 into that element. If you check the element and it contains -1, you know you've used it and you move to the next element to see if it's available (wrapping from 24 to 0 if necessary). Just off the top of my head. "BobbyS" <Bob***@discussions.microsoft.com> wrote in message Air code:news:6D620493-88F3-494B-B710-DF046E4CDA38@microsoft.com... > Using VB6 I have created an array using numbers (from 0 - 24). I want to > randomize the numbers to fill the array but if a duplicate number is > generated by the random function, disregard that number and go on to the > next > until I have 10 unique numbers. Any help would be greately appreciated. dim found as boolean dim n as integer dim rNum as integer dim count as integer dim ary() as integer redim ary(9) as integer do rNum = Int((10 - 0 + 1) * Rnd + 0) 'Int((upperbound - lowerbound + 1) * Rnd + lowerbound) for n = lbound(ary) to ubound(ary) if (rNum = ary(n)) then found = true exit for end if next if not found then ary(count) = rNum if count = ubound(ary) then exit do else count = count + 1 end if end if loop
Show quote
Hide quote
"Harry Strybos" <harry_NOSPAM@ffapaysmart.com.au> wrote in message
news:RKvDf.877$k6.16653@nasal.pacific.net.au... > "BobbyS" <Bob***@discussions.microsoft.com> wrote in message > news:6D620493-88F3-494B-B710-DF046E4CDA38@microsoft.com... >> Using VB6 I have created an array using numbers (from 0 - 24). I want to >> randomize the numbers to fill the array but if a duplicate number is >> generated by the random function, disregard that number and go on to the >> next >> until I have 10 unique numbers. Any help would be greately appreciated. > > Air code: > > dim found as boolean > dim n as integer > dim rNum as integer > dim count as integer > dim ary() as integer > > redim ary(9) as integer > > do > > rNum = Int((24 - 0 + 1) * Rnd + 0) 'Int((upperbound - lowerbound + 1) > * Rnd + lowerbound) > > for n = lbound(ary) to ubound(ary) > > if (rNum = ary(n)) then > found = true > exit for > end if > > next > > if not found then > > ary(count) = rNum > > if count = ubound(ary) then > exit do > else > count = count + 1 > end if > > end if > > loop > > Just use the code at http://vbnet.mvps.org/code/helpers/randomarray.htm,
modified for 25 numbers rather than 52 as the demo shows, then only reference the first 10 of the numbers. That's the simplest method. Otherwise, you have to loop and check, which, for small groups of numbers is fast, could become a problem for a very large array of numbers. Here's my crack at this the brute force way; first uncommented to show the size, then the same routine with comments. But I'd really still opt for the randomarray page method. Private Sub Form_Load() Randomize End Sub Private Sub Command1_Click() Dim ncount As Long Dim tmp As Long Dim cnt As Long Dim gotIt As Boolean Dim numDistinctValues As Long Dim numUpperValueMax As Long numDistinctValues = 10 'ie in a quiz, number of questions to ask numUpperValueMax = 25 'ie in a quiz, number of questions available ReDim narray(1 To numDistinctValues) For ncount = 1 To numDistinctValues gotIt = False Do Until gotIt = True tmp = Int(Rnd(1) * numUpperValueMax) + 1 If ncount = 1 Then narray(ncount) = tmp gotIt = True Else Do For cnt = 1 To ncount If tmp = narray(cnt) Then tmp = Int(Rnd(1) * numUpperValueMax) + 1 Exit For Else gotIt = True End If Next Loop Until gotIt = True End If If gotIt = True Then narray(ncount) = tmp Loop Next 'debug only List1.Clear For cnt = LBound(narray) To UBound(narray) List1.AddItem narray(cnt) Next End Sub 'Commented version with better debug code to show the workings ... Private Sub Command1_Click() Dim ncount As Long Dim tmp As Long Dim cnt As Long Dim gotIt As Boolean Dim numDistinctValues As Long Dim numUpperValueMax As Long 'ie in a quiz, number of questions to ask numDistinctValues = 10 'ie in a quiz, number of questions available numUpperValueMax = 25 ReDim narray(1 To numDistinctValues) List1.Clear 'seed the random number generator to 'assure a unique set of numbers each 'time the routine is run. The current 'system time is fine for this purpose. Randomize CSng(TimeValue(Time)) 'begin count to get numbers For ncount = 1 To numDistinctValues 'reset flag gotIt = False Do Until gotIt = True 'generate the number: 41 is the 'max number, 1 is the min. To reset 'the max, change the 41 to the required 'upper limit. tmp = Int(Rnd(1) * numUpperValueMax) + 1 'if its the first number, just add it 'and set the gotIt! flag If ncount = 1 Then narray(ncount) = tmp gotIt = True Else 'begin a loop that only ends 'once a new valid number is 'generated Do 'is it alread in the list? For cnt = 1 To ncount 'compare the current value '(tmp) to know values If tmp = narray(cnt) Then 'it must be there, so 'generate another number 'to try and exit the loop '---------------------------- 'DEBUG: 'show the duplicate number List1.AddItem ncount & vbTab & tmp & " is a dupe of #" & cnt '---------------------------- 'try to generate a different number tmp = Int(Rnd(1) * numUpperValueMax) + 1 '---------------------------- 'DEBUG: 'show the new number List1.AddItem ncount & vbTab & "new #" & ncount & " >" & tmp '---------------------------- Exit For Else gotIt = True 'no match! End If Next Loop Until gotIt = True End If If gotIt = True Then 'add to the array narray(ncount) = tmp '---------------------------- 'DEBUG: 'show the added number List1.AddItem ncount & vbTab & narray(ncount) '---------------------------- End If Loop Next End Sub -- Show quoteHide quoteRandy Birch MS MVP Visual Basic http://vbnet.mvps.org/ Please reply to the newsgroups so all can participate. "BobbyS" <Bob***@discussions.microsoft.com> wrote in message news:6D620493-88F3-494B-B710-DF046E4CDA38@microsoft.com... : Using VB6 I have created an array using numbers (from 0 - 24). I want to : randomize the numbers to fill the array but if a duplicate number is : generated by the random function, disregard that number and go on to the next : until I have 10 unique numbers. Any help would be greately appreciated.
How to best resize a form?
Using Control Array in User Control [function LOF] Fails after WordPad saving Proper way to resize a Form Why Randomize() won't work? VB6---.NET Problems with vbCtrlMask VB.NET Shutdown from text file can't use a function from dll built in delphi VB crash on .PopupMenu .zmnuDemo Defaultmenu |
|||||||||||||||||||||||