public static Collection < UnorderedPair > sampleUnorderedPairs ( int minI , int maxI , int minJ , int maxJ , double prop ) { int numI = maxI - minI ; int numJ = maxJ - minJ ; // Count the max number possible: long maxPairs = PairSampler . countUnorderedPairs ( minI , maxI , minJ , maxJ ) ; Collection < UnorderedPair > samples ; if ( maxPairs < 400000000 || prop > 0.1 ) { samples = new ArrayList < UnorderedPair > ( ) ; int min = Math . min ( minI , minJ ) ; int max = Math . max ( maxI , maxJ ) ; for ( int i = min ; i < max ; i ++ ) { for ( int j = i ; j < max ; j ++ ) { if ( ( minI <= i && i < maxI && minJ <= j && j < maxJ ) || ( minJ <= i && i < maxJ && minI <= j && j < maxI ) ) { if ( prop >= 1.0 || Prng . nextDouble ( ) < prop ) { samples . add ( new UnorderedPair ( i , j ) ) ; } } } } } else { samples = new HashSet < UnorderedPair > ( ) ; double numSamples = maxPairs * prop ; while ( samples . size ( ) < numSamples ) { int i = Prng . nextInt ( numI ) + minI ; int j = Prng . nextInt ( numJ ) + minJ ; if ( i <= j ) { // We must reject samples for which j < i, or else we would // be making pairs with i==j half as likely. samples . add ( new UnorderedPair ( i , j ) ) ; } } } return samples ; }
Sample with replacement unordered pairs of integers .