You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Othmen Tiliouine <ti...@gmail.com> on 2013/03/12 00:16:17 UTC

[Bag] random pick

Hello,

I just saw the Bag interface and its implementations, I'm surprised that Bag
<T> (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

Re: [Bag] random pick

Posted by Thomas Neidhart <th...@gmail.com>.
On 03/19/2013 10:38 PM, Othmen Tiliouine wrote:
> should i close the issue
> https://issues.apache.org/jira/browse/COLLECTIONS-448 ?

no, please leave it like this, the proposal is good, but it will
probably take some time to get processed.

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Bag] random pick

Posted by Othmen Tiliouine <ti...@gmail.com>.
should i close the issue
https://issues.apache.org/jira/browse/COLLECTIONS-448 ?

Re: [Bag] random pick

Posted by Othmen Tiliouine <ti...@gmail.com>.
yes with auxiliary array it is a good solution too

Re: [Bag] random pick

Posted by Othmen Tiliouine <ti...@gmail.com>.
2013/3/17 Ted Dunning <te...@gmail.com>

> Finally, the names should not consist of combinations of "pick" and
> "remit".  The correct English terms are "sample" and "replacement".
>

I suspected that the name was not good, it is because my English is poor
also, it was perhaps unnecessary to consider two methods 'pick ()' and '
pickAndRemit ()' becaufe 'pick' is nothing more than a combination of '
pickAndRemit ()' and 'remove ()' at source code, but I thought to the real bag
more than the java object's bag ', that's why I thought that bag must have
java in this method, I also saw that this need exists and when you google
'java bag' you will find many java bags that have a  method to get
random element,
maby the commons-collections bag does not have the same objective


I did not understand very well the rest of your post but I think the bag is
being best suited to sampling because it's powerful, you do not need to
loop over all elements, but only on the unique set
if you have an ensemble that contain 1000 x and 2000 y
sampling using an array or iterator will be ( for i from 0 to 2999) and
with bag (for i from 0 to 1)

Re: [Bag] random pick

Posted by Thomas Neidhart <th...@gmail.com>.
On 03/17/2013 03:02 AM, Ted Dunning wrote:
> I think that this implementation is a problem.
> 
> Bag implementations tend to fall into different categories, according to
> whether they provide unit (or log) time random access, random deletion and
> ordered traversal.  Most implementations don't provide unit time for all
> operations.
> 
> I think that your implementation assumes multiple operations have unit
> time.  Otherwise sampling all of the items will take quadratic time.
> 
> Also, your implementations alter the underlying collections.  That seems
> like a bad policy.
> 
> Sampling without replacement can be implemented as iteration over a
> permutation with unit amortized time.  A trivial implementation uses
> an auxiliary array.  It is also possible to do without the auxiliary.  You
> can get unit amortized time for linked list permutations as well, but it is
> difficult to do without the extra storage.
> 
> Finally, the names should not consist of combinations of "pick" and
> "remit".  The correct English terms are "sample" and "replacement".

additionally, I would prefer this feature to be a decorator rather than
altering the bag interface for it.

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Bag] random pick

Posted by Ted Dunning <te...@gmail.com>.
I think that this implementation is a problem.

Bag implementations tend to fall into different categories, according to
whether they provide unit (or log) time random access, random deletion and
ordered traversal.  Most implementations don't provide unit time for all
operations.

I think that your implementation assumes multiple operations have unit
time.  Otherwise sampling all of the items will take quadratic time.

Also, your implementations alter the underlying collections.  That seems
like a bad policy.

Sampling without replacement can be implemented as iteration over a
permutation with unit amortized time.  A trivial implementation uses
an auxiliary array.  It is also possible to do without the auxiliary.  You
can get unit amortized time for linked list permutations as well, but it is
difficult to do without the extra storage.

Finally, the names should not consist of combinations of "pick" and
"remit".  The correct English terms are "sample" and "replacement".

On Sat, Mar 16, 2013 at 6:25 PM, Othmen Tiliouine <
tiliouine.othmen@gmail.com> wrote:

> This is an example of use of pick from Bag
>
> https://github.com/influence160/flera
>
> see the classes
>
> https://github.com/influence160/flera/blob/master/flera-core/src/main/java/com/otiliouine/flera/SuccessionBasedWordGenerator.javaand
>
> https://github.com/influence160/flera/blob/master/flera-core/src/main/java/com/otiliouine/flera/analyzer/SuccessionBasedDictionaryAnalyzer.java
>
>
> 2013/3/13 Othmen Tiliouine <ti...@gmail.com>
>
> > I remplaced the patch
> >
> > 2013/3/13 Ted Dunning <te...@gmail.com>
> >
> >> You seem to have reformatted the entire file.  This makes it nearly
> >> impossible to review your suggested change.
> >>
> >> Can you make a diff that doesn't involve changing every line in the
> file?
> >>
> >> On Tue, Mar 12, 2013 at 3:48 PM, Othmen Tiliouine <
> >> tiliouine.othmen@gmail.com> wrote:
> >>
> >> > i puted the suggestion and attached the patch
> >> >
> >> > https://issues.apache.org/jira/browse/COLLECTIONS-448
> >> >
> >> > 2013/3/12 Thomas Neidhart <th...@gmail.com>
> >> >
> >> > > On 03/12/2013 08:58 AM, Othmen Tiliouine wrote:
> >> > > > Thank you Ted,
> >> > > >
> >> > > > I understand from your mail that there is no particular reason
> that
> >> > makes
> >> > > > that the interface Bag no contains these methods and that this
> >> subject
> >> > > has
> >> > > > never been discussed in the mailing list.
> >> > > >
> >> > > > I'll see if I can create this patch this weekend, but i want to
> >> know,
> >> > > what do
> >> > > > you think what are the methods I should add
> >> > > >
> >> > > > public Object pick(); //pick random element (with remove)
> >> > > > public Object pickAndRemit() ; //pick random element (without
> >> remove)
> >> > > > public Collection pick(int n); //pick random n element (with
> remove)
> >> > > > public Collection pickAndRemit(int n) ; //pick random n element
> >> > (without
> >> > > > remove)
> >> > > > public Iterator pick(int n); //pick random n element (with remove)
> >> > > > public Iterator pickAndRemit(int n) ; //pick random n element
> >> (without
> >> > > > remove)
> >> > > > public List pick(int n); //pick random n element (with remove)
> >> > > > public List pickAndRemit(int n) ; //pick random n element (without
> >> > > remove)
> >> > > > public Bag pick(int n); //pick random n element (with remove)
> >> > > > public Bag pickAndRemit(int n) ; //pick random n element (without
> >> > remove)
> >> > > >
> >> > > > maby i must provide the two kind of methods  ( Bag pick(int n) and
> >> > > Iterator
> >> > > > pickOrdered(int n) ) ?
> >> > > >
> >> > > >
> >> > > > there is something I do not understand why the bag does not use
> >> > generics
> >> > > ?
> >> > >
> >> > > the current version of collections in the trunk is already adapted
> to
> >> > > generics. We are currently in the process of preparing a release
> >> (4.0).
> >> > >
> >> > > So when you provide a patch, please align it to the version in the
> >> trunk.
> >> > >
> >> > > Thomas
> >> > >
> >> > >
> ---------------------------------------------------------------------
> >> > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> > > For additional commands, e-mail: dev-help@commons.apache.org
> >> > >
> >> > >
> >> >
> >>
> >
> >
>

Re: [Bag] random pick

Posted by Othmen Tiliouine <ti...@gmail.com>.
This is an example of use of pick from Bag

https://github.com/influence160/flera

see the classes
https://github.com/influence160/flera/blob/master/flera-core/src/main/java/com/otiliouine/flera/SuccessionBasedWordGenerator.javaand
https://github.com/influence160/flera/blob/master/flera-core/src/main/java/com/otiliouine/flera/analyzer/SuccessionBasedDictionaryAnalyzer.java


2013/3/13 Othmen Tiliouine <ti...@gmail.com>

> I remplaced the patch
>
> 2013/3/13 Ted Dunning <te...@gmail.com>
>
>> You seem to have reformatted the entire file.  This makes it nearly
>> impossible to review your suggested change.
>>
>> Can you make a diff that doesn't involve changing every line in the file?
>>
>> On Tue, Mar 12, 2013 at 3:48 PM, Othmen Tiliouine <
>> tiliouine.othmen@gmail.com> wrote:
>>
>> > i puted the suggestion and attached the patch
>> >
>> > https://issues.apache.org/jira/browse/COLLECTIONS-448
>> >
>> > 2013/3/12 Thomas Neidhart <th...@gmail.com>
>> >
>> > > On 03/12/2013 08:58 AM, Othmen Tiliouine wrote:
>> > > > Thank you Ted,
>> > > >
>> > > > I understand from your mail that there is no particular reason that
>> > makes
>> > > > that the interface Bag no contains these methods and that this
>> subject
>> > > has
>> > > > never been discussed in the mailing list.
>> > > >
>> > > > I'll see if I can create this patch this weekend, but i want to
>> know,
>> > > what do
>> > > > you think what are the methods I should add
>> > > >
>> > > > public Object pick(); //pick random element (with remove)
>> > > > public Object pickAndRemit() ; //pick random element (without
>> remove)
>> > > > public Collection pick(int n); //pick random n element (with remove)
>> > > > public Collection pickAndRemit(int n) ; //pick random n element
>> > (without
>> > > > remove)
>> > > > public Iterator pick(int n); //pick random n element (with remove)
>> > > > public Iterator pickAndRemit(int n) ; //pick random n element
>> (without
>> > > > remove)
>> > > > public List pick(int n); //pick random n element (with remove)
>> > > > public List pickAndRemit(int n) ; //pick random n element (without
>> > > remove)
>> > > > public Bag pick(int n); //pick random n element (with remove)
>> > > > public Bag pickAndRemit(int n) ; //pick random n element (without
>> > remove)
>> > > >
>> > > > maby i must provide the two kind of methods  ( Bag pick(int n) and
>> > > Iterator
>> > > > pickOrdered(int n) ) ?
>> > > >
>> > > >
>> > > > there is something I do not understand why the bag does not use
>> > generics
>> > > ?
>> > >
>> > > the current version of collections in the trunk is already adapted to
>> > > generics. We are currently in the process of preparing a release
>> (4.0).
>> > >
>> > > So when you provide a patch, please align it to the version in the
>> trunk.
>> > >
>> > > Thomas
>> > >
>> > > ---------------------------------------------------------------------
>> > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> > > For additional commands, e-mail: dev-help@commons.apache.org
>> > >
>> > >
>> >
>>
>
>

Re: [Bag] random pick

Posted by Othmen Tiliouine <ti...@gmail.com>.
I remplaced the patch

2013/3/13 Ted Dunning <te...@gmail.com>

> You seem to have reformatted the entire file.  This makes it nearly
> impossible to review your suggested change.
>
> Can you make a diff that doesn't involve changing every line in the file?
>
> On Tue, Mar 12, 2013 at 3:48 PM, Othmen Tiliouine <
> tiliouine.othmen@gmail.com> wrote:
>
> > i puted the suggestion and attached the patch
> >
> > https://issues.apache.org/jira/browse/COLLECTIONS-448
> >
> > 2013/3/12 Thomas Neidhart <th...@gmail.com>
> >
> > > On 03/12/2013 08:58 AM, Othmen Tiliouine wrote:
> > > > Thank you Ted,
> > > >
> > > > I understand from your mail that there is no particular reason that
> > makes
> > > > that the interface Bag no contains these methods and that this
> subject
> > > has
> > > > never been discussed in the mailing list.
> > > >
> > > > I'll see if I can create this patch this weekend, but i want to know,
> > > what do
> > > > you think what are the methods I should add
> > > >
> > > > public Object pick(); //pick random element (with remove)
> > > > public Object pickAndRemit() ; //pick random element (without remove)
> > > > public Collection pick(int n); //pick random n element (with remove)
> > > > public Collection pickAndRemit(int n) ; //pick random n element
> > (without
> > > > remove)
> > > > public Iterator pick(int n); //pick random n element (with remove)
> > > > public Iterator pickAndRemit(int n) ; //pick random n element
> (without
> > > > remove)
> > > > public List pick(int n); //pick random n element (with remove)
> > > > public List pickAndRemit(int n) ; //pick random n element (without
> > > remove)
> > > > public Bag pick(int n); //pick random n element (with remove)
> > > > public Bag pickAndRemit(int n) ; //pick random n element (without
> > remove)
> > > >
> > > > maby i must provide the two kind of methods  ( Bag pick(int n) and
> > > Iterator
> > > > pickOrdered(int n) ) ?
> > > >
> > > >
> > > > there is something I do not understand why the bag does not use
> > generics
> > > ?
> > >
> > > the current version of collections in the trunk is already adapted to
> > > generics. We are currently in the process of preparing a release (4.0).
> > >
> > > So when you provide a patch, please align it to the version in the
> trunk.
> > >
> > > Thomas
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > > For additional commands, e-mail: dev-help@commons.apache.org
> > >
> > >
> >
>

Re: [Bag] random pick

Posted by Ted Dunning <te...@gmail.com>.
You seem to have reformatted the entire file.  This makes it nearly
impossible to review your suggested change.

Can you make a diff that doesn't involve changing every line in the file?

On Tue, Mar 12, 2013 at 3:48 PM, Othmen Tiliouine <
tiliouine.othmen@gmail.com> wrote:

> i puted the suggestion and attached the patch
>
> https://issues.apache.org/jira/browse/COLLECTIONS-448
>
> 2013/3/12 Thomas Neidhart <th...@gmail.com>
>
> > On 03/12/2013 08:58 AM, Othmen Tiliouine wrote:
> > > Thank you Ted,
> > >
> > > I understand from your mail that there is no particular reason that
> makes
> > > that the interface Bag no contains these methods and that this subject
> > has
> > > never been discussed in the mailing list.
> > >
> > > I'll see if I can create this patch this weekend, but i want to know,
> > what do
> > > you think what are the methods I should add
> > >
> > > public Object pick(); //pick random element (with remove)
> > > public Object pickAndRemit() ; //pick random element (without remove)
> > > public Collection pick(int n); //pick random n element (with remove)
> > > public Collection pickAndRemit(int n) ; //pick random n element
> (without
> > > remove)
> > > public Iterator pick(int n); //pick random n element (with remove)
> > > public Iterator pickAndRemit(int n) ; //pick random n element (without
> > > remove)
> > > public List pick(int n); //pick random n element (with remove)
> > > public List pickAndRemit(int n) ; //pick random n element (without
> > remove)
> > > public Bag pick(int n); //pick random n element (with remove)
> > > public Bag pickAndRemit(int n) ; //pick random n element (without
> remove)
> > >
> > > maby i must provide the two kind of methods  ( Bag pick(int n) and
> > Iterator
> > > pickOrdered(int n) ) ?
> > >
> > >
> > > there is something I do not understand why the bag does not use
> generics
> > ?
> >
> > the current version of collections in the trunk is already adapted to
> > generics. We are currently in the process of preparing a release (4.0).
> >
> > So when you provide a patch, please align it to the version in the trunk.
> >
> > Thomas
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> > For additional commands, e-mail: dev-help@commons.apache.org
> >
> >
>

Re: [Bag] random pick

Posted by Othmen Tiliouine <ti...@gmail.com>.
i puted the suggestion and attached the patch

https://issues.apache.org/jira/browse/COLLECTIONS-448

2013/3/12 Thomas Neidhart <th...@gmail.com>

> On 03/12/2013 08:58 AM, Othmen Tiliouine wrote:
> > Thank you Ted,
> >
> > I understand from your mail that there is no particular reason that makes
> > that the interface Bag no contains these methods and that this subject
> has
> > never been discussed in the mailing list.
> >
> > I'll see if I can create this patch this weekend, but i want to know,
> what do
> > you think what are the methods I should add
> >
> > public Object pick(); //pick random element (with remove)
> > public Object pickAndRemit() ; //pick random element (without remove)
> > public Collection pick(int n); //pick random n element (with remove)
> > public Collection pickAndRemit(int n) ; //pick random n element (without
> > remove)
> > public Iterator pick(int n); //pick random n element (with remove)
> > public Iterator pickAndRemit(int n) ; //pick random n element (without
> > remove)
> > public List pick(int n); //pick random n element (with remove)
> > public List pickAndRemit(int n) ; //pick random n element (without
> remove)
> > public Bag pick(int n); //pick random n element (with remove)
> > public Bag pickAndRemit(int n) ; //pick random n element (without remove)
> >
> > maby i must provide the two kind of methods  ( Bag pick(int n) and
> Iterator
> > pickOrdered(int n) ) ?
> >
> >
> > there is something I do not understand why the bag does not use generics
> ?
>
> the current version of collections in the trunk is already adapted to
> generics. We are currently in the process of preparing a release (4.0).
>
> So when you provide a patch, please align it to the version in the trunk.
>
> Thomas
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [Bag] random pick

Posted by Thomas Neidhart <th...@gmail.com>.
On 03/12/2013 08:58 AM, Othmen Tiliouine wrote:
> Thank you Ted,
> 
> I understand from your mail that there is no particular reason that makes
> that the interface Bag no contains these methods and that this subject has
> never been discussed in the mailing list.
> 
> I'll see if I can create this patch this weekend, but i want to know, what do
> you think what are the methods I should add
> 
> public Object pick(); //pick random element (with remove)
> public Object pickAndRemit() ; //pick random element (without remove)
> public Collection pick(int n); //pick random n element (with remove)
> public Collection pickAndRemit(int n) ; //pick random n element (without
> remove)
> public Iterator pick(int n); //pick random n element (with remove)
> public Iterator pickAndRemit(int n) ; //pick random n element (without
> remove)
> public List pick(int n); //pick random n element (with remove)
> public List pickAndRemit(int n) ; //pick random n element (without remove)
> public Bag pick(int n); //pick random n element (with remove)
> public Bag pickAndRemit(int n) ; //pick random n element (without remove)
> 
> maby i must provide the two kind of methods  ( Bag pick(int n) and Iterator
> pickOrdered(int n) ) ?
> 
> 
> there is something I do not understand why the bag does not use generics ?

the current version of collections in the trunk is already adapted to
generics. We are currently in the process of preparing a release (4.0).

So when you provide a patch, please align it to the version in the trunk.

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [Bag] random pick

Posted by Othmen Tiliouine <ti...@gmail.com>.
Thank you Ted,

I understand from your mail that there is no particular reason that makes
that the interface Bag no contains these methods and that this subject has
never been discussed in the mailing list.

I'll see if I can create this patch this weekend, but i want to know, what do
you think what are the methods I should add

public Object pick(); //pick random element (with remove)
public Object pickAndRemit() ; //pick random element (without remove)
public Collection pick(int n); //pick random n element (with remove)
public Collection pickAndRemit(int n) ; //pick random n element (without
remove)
public Iterator pick(int n); //pick random n element (with remove)
public Iterator pickAndRemit(int n) ; //pick random n element (without
remove)
public List pick(int n); //pick random n element (with remove)
public List pickAndRemit(int n) ; //pick random n element (without remove)
public Bag pick(int n); //pick random n element (with remove)
public Bag pickAndRemit(int n) ; //pick random n element (without remove)

maby i must provide the two kind of methods  ( Bag pick(int n) and Iterator
pickOrdered(int n) ) ?


there is something I do not understand why the bag does not use generics ?

Othmen TILIOUINE.


2013/3/12 Ted Dunning <te...@gmail.com>

> 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
> > <T> (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
> >
>

Re: [Bag] random pick

Posted by Ted Dunning <te...@gmail.com>.
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
> <T> (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
>