SORU
24 EYLÜL 2008, ÇARŞAMBA


Toplama kümesinden rastgele bir eleman

Nasıl bir kümesinden rastgele bir eleman almak mı? Bir rasgele bir öğe seçmek ilgimi çekecek Ya da bir LinkedHashSet, Java HashSet. Diğer diller için çözümler de açığız.

CEVAP
24 EYLÜL 2008, ÇARŞAMBA


Biraz ilgili Biliyor Muydun:

Java yararlı yöntem vardır.açıklama.Karıştırma tüm koleksiyonları için koleksiyonlar:

/**
 * Randomly permutes the specified list using a default source of
 * randomness.  All permutations occur with approximately equal
 * likelihood.<p>
 *
 * The hedge "approximately" is used in the foregoing description because
 * default source of randomness is only approximately an unbiased source
 * of independently chosen bits. If it were a perfect source of randomly
 * chosen bits, then the algorithm would choose permutations with perfect
 * uniformity.<p>
 *
 * This implementation traverses the list backwards, from the last element
 * up to the second, repeatedly swapping a randomly selected element into
 * the "current position".  Elements are randomly selected from the
 * portion of the list that runs from the first element to the current
 * position, inclusive.<p>
 *
 * This method runs in linear time.  If the specified list does not
 * implement the {@link RandomAccess} interface and is large, this
 * implementation dumps the specified list into an array before shuffling
 * it, and dumps the shuffled array back into the list.  This avoids the
 * quadratic behavior that would result from shuffling a "sequential
 * access" list in place.
 *
 * @param  list the list to be shuffled.
 * @throws UnsupportedOperationException if the specified list or
 *         its list-iterator does not support the <tt>set</tt> operation.
 */
public static void shuffle(List<?> list) {
   ...
}

ve ayrıca

public static void shuffle(List<?> list, Random rnd) {
  ...
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Barnacules Nerdgasm

    Barnacules N

    20 Temmuz 2006
  • Christopher Bill

    Christopher

    30 NİSAN 2009
  • max2sims2

    max2sims2

    19 Kasım 2008