Velocity Reviews > Java > generating random array of pairs

# generating random array of pairs

puzzlecracker
Guest
Posts: n/a

 07-29-2008
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

RedGrittyBrick
Guest
Posts: n/a

 07-29-2008
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/Permuta...e_permutations

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

--
RGB

Jeff Higgins
Guest
Posts: n/a

 07-29-2008
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

puzzlecracker
Guest
Posts: n/a

 07-29-2008
On Jul 29, 10:01*am, Jeff Higgins <(E-Mail Removed)> 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...

Jeff Higgins
Guest
Posts: n/a

 07-29-2008
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>

puzzlecracker
Guest
Posts: n/a

 07-29-2008
On Jul 29, 10:31*am, Jeff Higgins <(E-Mail Removed)> 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.

Jeff Higgins
Guest
Posts: n/a

 07-29-2008
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
Guest
Posts: n/a

 07-29-2008
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>

puzzlecracker
Guest
Posts: n/a

 07-29-2008
On Jul 29, 11:47*am, Eric Sosman <(E-Mail Removed)> wrote:
> puzzlecracker wrote:
> > On Jul 29, 10:31 am, Jeff Higgins <(E-Mail Removed)> 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[i], 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 `<='.
>
> --
> (E-Mail Removed)

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
Guest
Posts: n/a

 07-29-2008
On Jul 29, 12:50*pm, puzzlecracker <(E-Mail Removed)> wrote:
> On Jul 29, 11:47*am, Eric Sosman <(E-Mail Removed)> wrote:
>
>
>
> > puzzlecracker wrote:
> > > On Jul 29, 10:31 am, Jeff Higgins <(E-Mail Removed)> 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[i], 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 `<='.

>
> > --
> > (E-Mail Removed)

>
> 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...