generating random array of pairs

Discussion in 'Java' started by puzzlecracker, Jul 29, 2008.

  1. Say, I want to generate an array of N number of distinct pairs, order
    of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    elements of is K, meaning that elements will be from 1 to K. So
    example is N 2, K=3,

    function will return Pair[2], where pairs could be {1,3} {2,3} or
    {1,2}{1,3}, etc... .

    Thanks
     
    puzzlecracker, Jul 29, 2008
    #1
    1. Advertising

  2. puzzlecracker wrote:
    > Say, I want to generate an array of N number of distinct pairs, order
    > of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    > elements of is K, meaning that elements will be from 1 to K. So
    > example is N 2, K=3,
    >
    > function will return Pair[2], where pairs could be {1,3} {2,3} or
    > {1,2}{1,3}, etc... .


    http://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations

    There is still not much actual Java content in your postings!

    --
    RGB
     
    RedGrittyBrick, Jul 29, 2008
    #2
    1. Advertising

  3. puzzlecracker

    Jeff Higgins Guest

    puzzlecracker wrote:
    > Say, I want to generate an array of N number of distinct pairs, order
    > of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    > elements of is K, meaning that elements will be from 1 to K.


    Well I've kinda said part of it under my breath,
    cause there are others here I don't want to distract.

    >So
    > example is N 2, K=3,
    >
    > function will return Pair[2], where pairs could be {1,3} {2,3} or
    > {1,2}{1,3}, etc... .
    >


    Generate a pool of all distinct pairs of elements in K.
    Randomly pick N.

    > Thanks
     
    Jeff Higgins, Jul 29, 2008
    #3
  4. On Jul 29, 10:01 am, Jeff Higgins <> wrote:
    > puzzlecracker wrote:
    > > Say, I want to generate an array of  N number of distinct pairs, order
    > > of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    > > elements of is K, meaning that elements will be from 1 to K.

    >
    > Well I've kinda said part of it under my breath,
    > cause there are others here I don't want to distract.
    >
    > >So
    > > example is N 2, K=3,

    >
    > > function will return Pair[2], where pairs could be {1,3} {2,3}  or
    > > {1,2}{1,3}, etc... .

    >
    > Generate a pool of all distinct pairs of elements in K.
    > Randomly pick N.
    >
    > > Thanks

    >
    >


    Good idea, how would you go about that?

    say all permutation of 2...is there a standard algorithms for that
    easily implemented in Java.. or better already written java algorithm
    for that...
     
    puzzlecracker, Jul 29, 2008
    #4
  5. puzzlecracker

    Jeff Higgins Guest

    puzzlecracker wrote:
    > Jeff Higgins wrote:
    >> puzzlecracker wrote:
    >>> Say, I want to generate an array of N number of distinct pairs, order
    >>> of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    >>> elements of is K, meaning that elements will be from 1 to K.

    >> Well I've kinda said part of it under my breath,
    >> cause there are others here I don't want to distract.
    >>
    >> Generate a pool of all distinct pairs of elements in K.
    >> Randomly pick N.
    >>

    >
    > Good idea, how would you go about that?
    >

    I don't know, what's an element, a Java int?

    > say all permutation of 2...is there a standard algorithms for that
    > easily implemented in Java.. or better already written java algorithm
    > for that...
    >


    <http://preview.tinyurl.com/5mzh5n>
     
    Jeff Higgins, Jul 29, 2008
    #5
  6. On Jul 29, 10:31 am, Jeff Higgins <> wrote:
    > puzzlecracker wrote:
    > > Jeff Higgins wrote:
    > >> puzzlecracker wrote:
    > >>> Say, I want to generate an array of  N number of distinct pairs, order
    > >>> of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    > >>> elements of is K, meaning that elements will be from 1 to K.
    > >> Well I've kinda said part of it under my breath,
    > >> cause there are others here I don't want to distract.

    >
    > >> Generate a pool of all distinct pairs of elements in K.
    > >> Randomly pick N.

    >
    > > Good idea, how would you go about that?

    >
    > I don't know, what's an element, a Java int?
    >
    > > say all permutation of 2...is there a standard algorithms for that
    > > easily implemented in Java.. or better already written java algorithm
    > > for that...

    >
    > <http://preview.tinyurl.com/5mzh5n>


    Yes, but we ought to do it for a general type T.
     
    puzzlecracker, Jul 29, 2008
    #6
  7. puzzlecracker

    Jeff Higgins Guest

    puzzlecracker wrote:
    Jeff Higgins wrote:

    >>> Good idea, how would you go about that?

    >> I don't know, what's an element, a Java int?
    >>
    >>> [snip]is there a standard algorithms for that
    >>> easily implemented in Java.. or better already written java algorithm
    >>> for that...

    >> <http://preview.tinyurl.com/5mzh5n>

    >
    > Yes, but we ought to do it for a general type T.


    OK
     
    Jeff Higgins, Jul 29, 2008
    #7
  8. puzzlecracker

    Jeff Higgins Guest

    Jeff Higgins wrote:
    > puzzlecracker wrote:
    > Jeff Higgins wrote:
    >
    >>>> Good idea, how would you go about that?
    >>> I don't know, what's an element, a Java int?
    >>>
    >>>> [snip]is there a standard algorithms for that
    >>>> easily implemented in Java.. or better already written java algorithm
    >>>> for that...
    >>> <http://preview.tinyurl.com/5mzh5n>

    >>
    >> Yes, but we ought to do it for a general type T.

    >
    > OK
    >

    <http://www.merriampark.com/comb.htm>
     
    Jeff Higgins, Jul 29, 2008
    #8
  9. On Jul 29, 11:47 am, Eric Sosman <> wrote:
    > puzzlecracker wrote:
    > > On Jul 29, 10:31 am, Jeff Higgins <> wrote:
    > >> puzzlecracker wrote:
    > >>> Jeff Higgins wrote:
    > >>>> puzzlecracker wrote:
    > >>>>> Say, I want to generate an array of  N number of distinct pairs, order
    > >>>>> of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    > >>>>> elements of is K, meaning that elements will be from 1 to K.
    > >>>> Well I've kinda said part of it under my breath,
    > >>>> cause there are others here I don't want to distract.
    > >>>> Generate a pool of all distinct pairs of elements in K.
    > >>>> Randomly pick N.
    > >>> Good idea, how would you go about that?
    > >> I don't know, what's an element, a Java int?

    >
    > >>> say all permutation of 2...is there a standard algorithms for that
    > >>> easily implemented in Java.. or better already written java algorithm
    > >>> for that...
    > >> <http://preview.tinyurl.com/5mzh5n>

    >
    > > Yes, but we ought to do it for a general type T.

    >
    >         T[] pool = ...;
    >         Pair[] allPairs = new Pair[pool.length * (pool.length - 1) / 2];
    >         for (int k = 0, i = 0;  i < pool.length;  ++i) {
    >             for (int j = 0;  j < i;  ++j)
    >                 allPairs[k++] = new Pair(pool, pool[j]);
    >         }
    >
    > This excludes pairs like (2,2), (3,3), and so on.  If you want them
    > included, change the `-' to `+' and the second `<' to `<='.
    >
    > --
    >


    Let me see if it doesn't create duplicates such as {1,2} and {2,1},
    otherwise code looks intelligently incomprehensible :)

    Thanks for the effort.
     
    puzzlecracker, Jul 29, 2008
    #9
  10. On Jul 29, 12:50 pm, puzzlecracker <> wrote:
    > On Jul 29, 11:47 am, Eric Sosman <> wrote:
    >
    >
    >
    > > puzzlecracker wrote:
    > > > On Jul 29, 10:31 am, Jeff Higgins <> wrote:
    > > >> puzzlecracker wrote:
    > > >>> Jeff Higgins wrote:
    > > >>>> puzzlecracker wrote:
    > > >>>>> Say, I want to generate an array of  N number of distinct pairs, order
    > > >>>>> of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    > > >>>>> elements of is K, meaning that elements will be from 1 to K.
    > > >>>> Well I've kinda said part of it under my breath,
    > > >>>> cause there are others here I don't want to distract.
    > > >>>> Generate a pool of all distinct pairs of elements in K.
    > > >>>> Randomly pick N.
    > > >>> Good idea, how would you go about that?
    > > >> I don't know, what's an element, a Java int?

    >
    > > >>> say all permutation of 2...is there a standard algorithms for that
    > > >>> easily implemented in Java.. or better already written java algorithm
    > > >>> for that...
    > > >> <http://preview.tinyurl.com/5mzh5n>

    >
    > > > Yes, but we ought to do it for a general type T.

    >
    > >         T[] pool = ...;
    > >         Pair[] allPairs = new Pair[pool.length * (pool.length - 1) / 2];
    > >         for (int k = 0, i = 0;  i < pool.length;  ++i) {
    > >             for (int j = 0;  j < i;  ++j)
    > >                 allPairs[k++] = new Pair(pool, pool[j]);
    > >         }

    >
    > > This excludes pairs like (2,2), (3,3), and so on.  If you want them
    > > included, change the `-' to `+' and the second `<' to `<='.

    >
    > > --
    > >

    >
    > Let me see if it doesn't create duplicates such as {1,2} and {2,1},
    > otherwise code looks intelligently incomprehensible :)
    >
    > Thanks for the effort.


    Argh, it will have repetitions...
     
    puzzlecracker, Jul 29, 2008
    #10
  11. puzzlecracker

    Lew Guest

    On Jul 29, 1:31 pm, puzzlecracker <> wrote:
    > On Jul 29, 12:50 pm, puzzlecracker <> wrote:
    >
    >
    >
    > > On Jul 29, 11:47 am, Eric Sosman <> wrote:

    >
    > > > puzzlecracker wrote:
    > > > > On Jul 29, 10:31 am, Jeff Higgins <> wrote:
    > > > >> puzzlecracker wrote:
    > > > >>> Jeff Higgins wrote:
    > > > >>>> puzzlecracker wrote:
    > > > >>>>> Say, I want to generate an array of  N number of distinct pairs, order
    > > > >>>>> of each element is irrelevant ({1.5} is the same as {5,1}). Pool of
    > > > >>>>> elements of is K, meaning that elements will be from 1 to K.
    > > > >>>> Well I've kinda said part of it under my breath,
    > > > >>>> cause there are others here I don't want to distract.
    > > > >>>> Generate a pool of all distinct pairs of elements in K.
    > > > >>>> Randomly pick N.
    > > > >>> Good idea, how would you go about that?
    > > > >> I don't know, what's an element, a Java int?

    >
    > > > >>> say all permutation of 2...is there a standard algorithms for that
    > > > >>> easily implemented in Java.. or better already written java algorithm
    > > > >>> for that...
    > > > >> <http://preview.tinyurl.com/5mzh5n>

    >
    > > > > Yes, but we ought to do it for a general type T.

    >
    > > >         T[] pool = ...;
    > > >         Pair[] allPairs = new Pair[pool.length * (pool.length - 1) / 2];
    > > >         for (int k = 0, i = 0;  i < pool.length;  ++i) {
    > > >             for (int j = 0;  j < i;  ++j)
    > > >                 allPairs[k++] = new Pair(pool, pool[j]);
    > > >         }

    >
    > > > This excludes pairs like (2,2), (3,3), and so on.  If you want them
    > > > included, change the `-' to `+' and the second `<' to `<='.

    >
    > > > --
    > > >

    >
    > > Let me see if it doesn't create duplicates such as {1,2} and {2,1},
    > > otherwise code looks intelligently incomprehensible :)

    >
    > > Thanks for the effort.

    >
    > Argh, it will have repetitions...


    Then it behooves you to take the excellent start you've been given and
    put some effort of your own into modifying it to do what you want.

    --
    Lew
     
    Lew, Jul 29, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    524
    bugbear
    Nov 17, 2006
  2. globalrev
    Replies:
    4
    Views:
    810
    Gabriel Genellina
    Apr 20, 2008
  3. Greg Willits

    Array of Pairs data structure?

    Greg Willits, Oct 25, 2007, in forum: Ruby
    Replies:
    3
    Views:
    137
    Rick DeNatale
    Oct 25, 2007
  4. Andy

    Identify new pairs in an array

    Andy, Mar 26, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    199
    Joe Smith
    Mar 27, 2006
  5. VK
    Replies:
    15
    Views:
    1,321
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page