permutation

  • Thread starter Thread starter user
  • Start date Start date
U

user

Hello
I have Array of 50 ints. I want to receive random permutation, so
in each int will be different number from 0-49.
Is there any class for permutation ?

Thanx
Michal
 
Hi,

I'm not completely sure I understand your question.
I don't think there are any Permutation classes in .Net,
but will this code give you what you seek?

Random rand = new Random();


ArrayList numbers = new ArrayList();
int[] permutation = new int[50];

// create a list that holds the numbser 0, 1, 2 ... 49
for(int i = 0; i < permutation.Length; i++)
{
numbers.Add(i);
}

// for each entry in the permutation list
// grab the number from a random position in the number list
for(int i = 0; i < permutation.Length; i++)
{
int o = rand.Next(numbers.Count);
permutation = (int)numbers[o];
numbers.RemoveAt(o);
}

Happy coding!
Morten Wennevik [C# MVP]
 

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

Back
Top