generating random array of pairs

P

puzzlecracker

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
 
R

RedGrittyBrick

puzzlecracker said:
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!
 
J

Jeff Higgins

puzzlecracker said:
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.
 
P

puzzlecracker

puzzlecracker said:
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.

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

puzzlecracker

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

puzzlecracker

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

Lew

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top