For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > January 2006 > Eliminate duplication in an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Eliminate duplication in an array
BobbyS

2006-01-30, 6:55 pm

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.
Karl E. Peterson

2006-01-30, 6:55 pm

BobbyS wrote:
> 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?
--
Working without a .NET?
http://classicvb.org/


Jeff Johnson [MVP: VB]

2006-01-30, 6:55 pm


"BobbyS" <BobbyS@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.


You could create a second array of 25 elements and assign the numbers 0 to
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.


Harry Strybos

2006-01-30, 6:55 pm

"BobbyS" <BobbyS@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((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


Jeff Johnson [MVP: VB]

2006-01-30, 6:55 pm


"Karl E. Peterson" <karl@mvps.org> wrote in message
news:OTahVPeJGHA.740@TK2MSFTNGP12.phx.gbl...

>
> Has the teacher suggested any potential methods to solve this?


Egad, if I just gave someone the answer to a homework question, kick me.


Harry Strybos

2006-01-30, 6:55 pm

"Harry Strybos" <harry_NOSPAM@ffapaysmart.com.au> wrote in message
news:RKvDf.877$k6.16653@nasal.pacific.net.au...
> "BobbyS" <BobbyS@discussions.microsoft.com> wrote in message
> news:6D620493-88F3-494B-B710-DF046E4CDA38@microsoft.com...
>
> 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
>
>



Karl E. Peterson

2006-01-30, 6:55 pm

Jeff Johnson [MVP: VB] wrote:
> "Karl E. Peterson" <karl@mvps.org> wrote in message
> news:OTahVPeJGHA.740@TK2MSFTNGP12.phx.gbl...
>
>
> Egad, if I just gave someone the answer to a homework question, kick
> me.


<g>
--
Working without a .NET?
http://classicvb.org/


BobbyS

2006-01-30, 6:55 pm

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:
>
> <g>
> --
> Working without a .NET?
> http://classicvb.org/
>
>
>

MikeD

2006-01-30, 6:55 pm


I guess we don't have to kick *Jeff* then. Dammit! <g>

--
Mike
Microsoft MVP Visual Basic


"BobbyS" <BobbyS@discussions.microsoft.com> wrote in message
news:19DED5BB-2105-438D-982A-1DB599FBD224@microsoft.com...[color=darkred]
> 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:
>


Randy Birch

2006-01-30, 6:55 pm

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



--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.




"BobbyS" <BobbyS@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.

Karl E. Peterson

2006-01-30, 9:55 pm

MikeD wrote:
> I guess we don't have to kick *Jeff* then. Dammit! <g>


LOL!
--
Working without a .NET?
http://classicvb.org/


Jeff Johnson [MVP: VB]

2006-01-31, 6:55 pm


"MikeD" <nobody@nowhere.edu> wrote in message
news:u6yx62fJGHA.916@TK2MSFTNGP10.phx.gbl...

> I guess we don't have to kick *Jeff* then. Dammit! <g>


PPPHHHHBBBBTTTT!!!!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com