Hi Yehuda,
just being curious, what do think about your and my solution regarding
performance and resources? I think collections take up quiet a lot of
resources, so performance shouldn't be very good either.
Did I understand your solution right, you are doing another extra loop to
generate four numbers and then drop one? Why this? I'm wondering, because
you were the one suggesting random numbers between 3 and 18.
Just being curious ;)
> Of course the other solution is to use a collection. Then just compare
> the first member of the collection to the new number. If the new number
> is smaller add it into the collection before the first member. When you
> have the fourth number delete the number that is listed first from the
> collection. In general, if I can get away with it I prefer collections
> to arrays.
>
> -----Original Message-----
> From: boris.gruening@g... [mailto:boris.gruening@g...]
> Sent: Wednesday, October 24, 2001 4:57 AM
> To: professional vb
> Subject: [pro_vb] RE: Integer array
>
>
> I agree with Yehuda, so how about this shorty:
>
> Dim aResult() As Integer
> Dim aTmp() As Integer
> Dim iNumMax, iRndMin, iRndMax As Integer
> Dim i, j, k As Integer
>
> 'Initialize parameters
> iNumMax = 6 'How many numbers do you want
> iRndMin = 3 'What is the lowest possible value
> iRndMax = 18 'What is the highest possible value
> ReDim aResult(iNumMax) 'Resize the result array
> ReDim aTmp(iRndMax) 'Resize the temp array
>
> 'Seed the random number generator
> Randomize
> For i = 1 To UBound(aResult)
> 'Generate random number
> j = Rnd() * (iRndMax - iRndMin) + iRndMin
> 'Count occurance in temp array
> aTmp(j) = aTmp(j) + 1
> Next i
>
> 'Now we have sorted random numbers
> 'in a coded form
>
> 'Decode and compress temp array
> i = 1
> 'Look for occurance of random numbers in temp array
> For j = 1 To UBound(aTmp)
> If aTmp(j) > 0 Then
> 'We have found at least one occurance
> For k = 1 To aTmp(j)
> aResult(i) = j
> i = i + 1
> Next k
> End If
> Next j
>
> 'Now we have 6 sorted random numbers between 3 and 18
> 'Just change the parameters and ....
>
> Boris
>