Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AA591DE41 for ; Mon, 11 Mar 2013 23:54:49 +0000 (UTC) Received: (qmail 73231 invoked by uid 500); 11 Mar 2013 23:54:49 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 73148 invoked by uid 500); 11 Mar 2013 23:54:48 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 73070 invoked by uid 99); 11 Mar 2013 23:54:46 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Mar 2013 23:54:46 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of ted.dunning@gmail.com designates 209.85.223.181 as permitted sender) Received: from [209.85.223.181] (HELO mail-ie0-f181.google.com) (209.85.223.181) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Mar 2013 23:54:39 +0000 Received: by mail-ie0-f181.google.com with SMTP id 17so5622789iea.12 for ; Mon, 11 Mar 2013 16:54:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=BY9PP8UQBoKAfErOZfgtLQZ2hqScnMpUNb4ACqWPpEY=; b=hmZZJUdl01KBGDQNfAUl1rRG2lRkMXeN4km/Qcf1ecuH7Xqj/I48dBYotgHDVeoe5+ of3KBfR9i9kHEmNSRwByjs5LVsvemeZA2JVTwlF+zk6sLmQaJxSylB9uzq3rHduxXeXn /OdDPOmr3Z4WmV5iisBFdbY5WYHHbKRxJv76cosAfdsxmobabA7EWLCPVjI7y3MtD/ES QORlbcn18Ltr6Ey1VVGXUe3bYoIgYXVsdPoo2TYOlNa/jg4cd0rsv1oha3HTGkWWe4e5 S686cew0KUz81XfXO36izXZAVROegztmmoCT4znbsza/YjRur6pScv+K+2cf0Qywsqha wqaA== X-Received: by 10.50.186.227 with SMTP id fn3mr9576719igc.17.1363046058442; Mon, 11 Mar 2013 16:54:18 -0700 (PDT) MIME-Version: 1.0 Received: by 10.64.106.74 with HTTP; Mon, 11 Mar 2013 16:53:48 -0700 (PDT) In-Reply-To: References: From: Ted Dunning Date: Mon, 11 Mar 2013 16:53:48 -0700 Message-ID: Subject: Re: [Bag] random pick To: Commons Developers List Content-Type: multipart/alternative; boundary=14dae93408a5204a4504d7aee452 X-Virus-Checked: Checked by ClamAV on apache.org --14dae93408a5204a4504d7aee452 Content-Type: text/plain; charset=UTF-8 Othmen, The common way to contribute code is to file a bug report/enhancement request at the correct commons component: https://issues.apache.org/jira/secure/BrowseProjects.jspa#10260 My guess is that you want collections which is at https://issues.apache.org/jira/browse/COLLECTIONS Then you should put your suggested solution onto that JIRA as an attachment. The attachment should be a patch file. That will be a place that the merits of the contribution can be discussed. My own comment here is that the common idiom in the English statistical literature for sampling from a bag is either sampling without replacement or sampling with replacement. Moreover, it is typical that you allow for multiple items to be sampled rather than requiring sampling to proceed one element at a time. Sampling n items from an n-item bag without replacement, for instance, would return a permutation of the bag (if you get an ordered sample) or a partition of the bag (if you get an unordered sample). There is also the question of whether the bag should be considered immutable during sampling. If you want to leave the bag unchanged by sampling, then you probably should be returning a sampler object of some kind that is kind of a randomized iterator or iterable. These kinds of design decisions need to be hashed out in the process of getting your contribution into the code. Good luck with your contribution! On Mon, Mar 11, 2013 at 4:16 PM, Othmen Tiliouine < tiliouine.othmen@gmail.com> wrote: > Hello, > > I just saw the Bag interface and its implementations, I'm surprised that > Bag > (and none of these implementations) expose the method > public T pick() and public T pickAndRemit() (pick a random element) > The Bag object we see in nature, it is mainly used to this, that's why > it is widely > used in the probability that when I met 2 white balls and one black, when I > draw a ball randomly I have 2 times more likely to have a white ball > > I think that if this caracteristic exists it would be very valuable. > > > this is a simple implementation of pick() and pickAndRemit() with HashBag > > package com.otiliouine.commons.collections; > > import java.util.Iterator; > > import org.apache.commons.collections.bag.HashBag; > > public class OpaqueHashBag extends HashBag implements OpaqueBag { > > public Object pcik() { > if (size() == 0) { > return null; > } > int randomIndex = (int) (Math.random() * size()); > int searchIndex = randomIndex; > > Iterator iterator = this.iterator(); > //iterator = this.map.keySet().iterator() > Object selectedItem = iterator.next(); > int count; > > while (searchIndex > 0) { > searchIndex --; > selectedItem = iterator.next(); > } > // while ((count = getCount(selectedItem)) < searchIndex + 1) { > // searchIndex -= count; > // selectedItem = iterator.next(); > // } > return selectedItem; > } > > public Object pickAndRemit() { > Object picked = pcik(); > if (picked != null) { > remove(picked, 1); > } > return picked; > } > } > > > > it can be optimized if it is writen in AbstractMapBag class > > and this is the test > > public class TestOpaqueHashBag { > > private OpaqueHashBag bag; > > public static void main(String ... args) { > TestOpaqueHashBag t = new TestOpaqueHashBag(); > t.before(); > t.testpick(); > } > > public void before(){ > bag = new OpaqueHashBag(); > bag.add("white", 6); > bag.add("black", 3); > bag.add("red", 1); > } > > public void testpick() { > int w = 0, b = 0, r = 0; > for (int i = 0; i < 1000; i++) { > String ball = (String) bag.pcik(); > > if (ball.equals("white")) { > w ++; > } else if (ball.equals("black")) { > b ++; > } else { > r ++; > } > } > System.out.println("%white = " + w/10); > System.out.println("%black = " + b/10); > System.out.println("%red = " + r/10); > } > > } > > output : > > %white = 59 > %black = 30 > %red = 9 > > > Othmen TILIOUINE > --14dae93408a5204a4504d7aee452--