andricom

06-18-2012, 11:56 AM

Hi,

I'll try to explain what I was trying to do with this functions.

say you have four pairs of things, pair 1-1, 2-2, 3-3 and 4-4, and you want to swap one element of each pair and give it to another

example

1-4, 2-3, 3-1, 4,2

the only thing I want to make sure is:

1. No element can be paired with 'himself' so no '1-1 or 2-2' after the function.

and 2. No repeating assigned elements, so no '1-3, 2-3' after the function.

So this would be a way of permute numbers.

an this is how I tryed it:

value = document.myform.three.value;

var x = new Array(value);

for(a=0;a<value;a++){

x[a]= 0;

}

function itera(){

value = document.myform.three.value;

for(var a=0; a<value; a++){

x[a] = Math.floor(Math.random()*value);

for (var b=a; b>=0; b=b-1){

if (x[a] == x || x[a]==a){

do {

x[a]= Math.floor(Math.random()*value);

}

while(x[a] == a);

b=a;

}

else {

}

}

being 'Value' the number of pairs to permute, which is an external input.

This function works fine some times but [B]sometimes it crashes and I can't figure out why

I'd apreciate some help please. thanks.

PD: I don't care whether there is a specific function to permute numbers, I'd simply like to understand whats wrong with this one.

I'll try to explain what I was trying to do with this functions.

say you have four pairs of things, pair 1-1, 2-2, 3-3 and 4-4, and you want to swap one element of each pair and give it to another

example

1-4, 2-3, 3-1, 4,2

the only thing I want to make sure is:

1. No element can be paired with 'himself' so no '1-1 or 2-2' after the function.

and 2. No repeating assigned elements, so no '1-3, 2-3' after the function.

So this would be a way of permute numbers.

an this is how I tryed it:

value = document.myform.three.value;

var x = new Array(value);

for(a=0;a<value;a++){

x[a]= 0;

}

function itera(){

value = document.myform.three.value;

for(var a=0; a<value; a++){

x[a] = Math.floor(Math.random()*value);

for (var b=a; b>=0; b=b-1){

if (x[a] == x || x[a]==a){

do {

x[a]= Math.floor(Math.random()*value);

}

while(x[a] == a);

b=a;

}

else {

}

}

being 'Value' the number of pairs to permute, which is an external input.

This function works fine some times but [B]sometimes it crashes and I can't figure out why

I'd apreciate some help please. thanks.

PD: I don't care whether there is a specific function to permute numbers, I'd simply like to understand whats wrong with this one.