public static ModifiableDBIDs randomSample(DBIDs source,int k,Random random){
  if (k < 0 || k > source.size()) {
    throw new IllegalArgumentException("Illegal value for size of random sample: " + k + " > "+ source.size()+ " or < 0");
  }
  if (random == null) {
    random=new FastNonThreadsafeRandom();
  }
  if (k < source.size() >> 1) {
    ArrayDBIDs aids=DBIDUtil.ensureArray(source);
    DBIDArrayIter iter=aids.iter();
    HashSetModifiableDBIDs sample=DBIDUtil.newHashSet(k);
    while (sample.size() < k) {
      iter.seek(random.nextInt(aids.size()));
      sample.add(iter);
    }
    return sample;
  }
 else {
    ArrayModifiableDBIDs sample=DBIDUtil.newArray(source);
    randomShuffle(sample,random,k);
    for (int i=sample.size() - 1; i >= k; i--) {
      sample.remove(i);
    }
    return sample;
  }
}

public static ModifiableDBIDs randomSample ( DBIDs source , int k , Random random ) { if ( k < 0 || k > source . size ( ) ) { throw new IllegalArgumentException ( "Illegal value for size of random sample: " + k + " > " + source . size ( ) + " or < 0" ) ; } if ( random == null ) { random = new FastNonThreadsafeRandom ( ) ; } if ( k < source . size ( ) > > 1 ) { ArrayDBIDs aids = DBIDUtil . ensureArray ( source ) ; DBIDArrayIter iter = aids . iter ( ) ; HashSetModifiableDBIDs sample = DBIDUtil . newHashSet ( k ) ; while ( sample . size ( ) < k ) { iter . seek ( random . nextInt ( aids . size ( ) ) ) ; sample . add ( iter ) ; } return sample ; } else { ArrayModifiableDBIDs sample = DBIDUtil . newArray ( source ) ; randomShuffle ( sample , random , k ) ; for ( int i = sample . size ( ) - 1 ; i >= k ; i -- ) { sample . remove ( i ) ; } return sample ; } }
produce a random sample of the given dbids .
