Function IsPrime2(Number As Double) As Variant
Dim x As Long, dbl As Double, lngUp As Long
'''' Author Dave Braden et al, posted to MS Excel Newsgroups
'''' http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=
'''' off&selm=375716DB.51808223%40fiastl.net&rnum=10
If Number <> Int(Number) Then
IsPrime2 = CVErr(xlErrValue)
Else
Number = Abs(Number)
If Number < 2 Then
IsPrime2 = False
ElseIf Number / 2 = Int(Number / 2) Then
IsPrime2 = Number = 2
ElseIf Number < 9 Then
IsPrime2 = True
Else
lngUp = Int(Sqr(Number))
IsPrime2 = True
For x = 3 To lngUp Step 2
dbl = Number / x
If dbl = Int(dbl) Then
IsPrime2 = False
Exit For
End If
Next x
End If
End If
End Function
Function NonPrimeSampleNoReplace(SampleSize As Long, LowerBound As Long, _
UpperBound As Long, Optional AlreadyUsedArray)
'''' Author: Jay Petrulis
Dim PopulationCollection As New Collection
Dim SampleArray() As Long
Dim temp As Long
Dim i As Long
Dim j As Long
Randomize
If UpperBound < LowerBound Then
temp = UpperBound
UpperBound = LowerBound
LowerBound = temp
End If
If (SampleSize > (UpperBound - LowerBound + 1) Or SampleSize <= 0) Then
NonPrimeSampleNoReplace = CVErr(xlErrValue)
Exit Function
End If
ReDim SampleArray(1 To SampleSize) As Long
For i = LowerBound To UpperBound
If IsMissing(AlreadyUsedArray) Then
If Not IsPrime2(CDbl(i)) Then
PopulationCollection.Add i
End If
Else
If IsError(Application.Match(i, AlreadyUsedArray, 0)) Then
If Not IsPrime2(CDbl(i)) Then
PopulationCollection.Add i
End If
End If
End If
Next i
For i = 1 To SampleSize
With PopulationCollection
j = Int(Rnd * .Count) + 1
SampleArray(i) = .Item(j)
.Remove j
End With
Next i
Set PopulationCollection = Nothing
NonPrimeSampleNoReplace = Application.Transpose(SampleArray)
End Function