Optimized Algorithm to Find All 720 Random Permutations of [0, 6)

In summary, the conversation discusses an algorithm for finding all random permutations of n=[0,6) and the need for an optimized code to generate all 720 permutations. The conversation also mentions existing libraries and different approaches for generating random permutations, including using factoradic or Lehmer code. The final solution involves creating a methodical non-random list of permutations and then putting them into a random order.
  • #1
intervoxel
195
1
TL;DR Summary
Algorithm do find all random permutations of n=[0,6)
Summary: Algorithm do find all random permutations of n=[0,6)

Hi,

The following algorithm gives 6 out of the 720 random permutations of integers in the range [0, 6).

Code:
 t=0..5                             // permutation
        i=0..5                      // sequence index
            n = (t + i) % 6;        // % is the mod operator
            n = n*n % 11;           // 11 is prime
            n += n>>3;              // >> is the bit shift right operator
            n = n & 7;              // & is the bitwise AND operator

I need an optimized code that gives all the 720 random permutations:

Code:
    t=0..719                    // permutation
        i=0..5                  // sequence index
            n = f(t, i);

Thanks for any answer.
 
Technology news on Phys.org
  • #2
Should move to computer forum.
 
  • #3
What language are you using? or is that pseudo code in your example?
 
  • #4
Optimised how, and why?
  • Least number of statements?
  • Lowest memory use?
  • Fastest execution?
  • Most random?
  • Most maintanable?
And what do you mean by random?

The easiest way to answer these questions is to write the code yourself (hint: generate all the 720 permutations and then sort the numbers 0..719 into a random order).
 
  • Like
Likes jedishrfu
  • #5
There are existing libraries that can do it, you can check how they do it.
There are recursive algorithms (given a permutation, find the next one e.g. when sorted like a lexicon) and direct algorithms (give me the nth permutation in this order).
 
  • #6
Thank you for the kind answers, guys.

I found out exactly what I needed (c language):

Random permutations:
int idx = 6;
unsigned char permutation[] = { 0, 1, 2, 3, 4, 5 };
unsigned char temp[6];

void swap(unsigned char *a, unsigned char *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/*
 * Prints a different permutation.
 */
void next()
{
    while(true)
    {
        if(idx == 6)
        {
            int i;
            for(i = 0; i < 6; i++)
                temp[i] = 0;
            idx = 0;
        }
        if(temp[idx] < idx)
        {
            if(idx % 2 == 0)
                swap(&permutation[0], &permutation[idx]);
            else
                swap(&permutation[temp[idx]], &permutation[idx]);
            permutation[idx]++;
            idx = 0;
            //
            printArray(permutation);
            return;
        }
        else
        {
            temp[idx] = 0;
            idx++;
        }
    }
}

Every time function next() is called, it prints a different permutation.
 
  • #7
mfb said:
direct algorithms (give me the nth permutation in this order).

Can you point me at one of those?
 
  • #8
Vanadium 50 said:
Can you point me at one of those?
Do you have Knuth vol 3? Or search for 'factoradic' or 'Lehmer code'. For only 6 elements this is not worth it of course.

It turns out that the OP's requirement was actually much simpler than it seemed from his OP.
 
  • Like
Likes mfb
  • #9
pbuk said:
Do you have Knuth vol 3?

I don't, and that was my first thought. :) But thanks, Lehmer code is exactly what I need.
 
  • #10
Just to clarify, do you mean to determine all possible permutations and put them in a random order? If so, I would do it in exactly those two steps.
1) Make a methodical non-random list of all possible permutations.
2) Put that list into a random order.

On the other hand, suppose you want to generate a sequence of random permutations, with all permutations equally likely, and allowing repeats. The is an entirely different problem. But you can still make the list of step 1) above and repeatedly select one at random as long as you like.
 

1. What is the purpose of finding all 720 random permutations of [0,6)?

The purpose of finding all 720 random permutations of [0,6) is to determine the different ways in which the numbers 0 to 6 can be arranged in a random order. This can be useful in various mathematical and statistical analyses, as well as in creating randomized sequences for experiments or simulations.

2. How is the algorithm optimized?

The algorithm is optimized by using efficient data structures and algorithms, such as backtracking and recursion, to minimize the number of computations and memory usage. This allows for faster and more efficient execution of the algorithm, making it suitable for finding all 720 random permutations in a reasonable amount of time.

3. Can the algorithm find permutations of numbers other than [0,6)?

Yes, the algorithm can be modified to find permutations of any set of numbers. However, the number of permutations may vary depending on the size of the set. For example, if the set contains n numbers, there will be n! (n factorial) permutations.

4. How accurate are the results of the algorithm?

The results of the algorithm are highly accurate as it considers all possible combinations of the numbers in [0,6) and does not miss any permutation. However, the order of the permutations may vary depending on the implementation of the algorithm.

5. How can the algorithm be applied in real-world scenarios?

The algorithm can be applied in various real-world scenarios, such as in creating randomized sequences for scientific experiments, in cryptography for generating secure random numbers, and in data analysis for shuffling data. It can also be used in programming tasks that involve generating all possible combinations of a set of numbers.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
  • Precalculus Mathematics Homework Help
Replies
4
Views
747
  • General Math
Replies
2
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
Replies
5
Views
2K
Back
Top