-3

There are many different instructions to do random permutation on the Internet, but they all use libraries. Is there any way to do random permutation without using any built-in library? I know how to generate random numbers using Math.random(). Does generating random permutation have to do with Math.random()? Shuffling looks so complicated to me.

My goal is if I run the programme by 'java randompermutation 3', then the programme returns either 1,2,3 or 1,3,2, or 3,1,2, or 3,2,1, or 2,3,1, or 2,1,3

baitmbarek
  • 2,440
  • 4
  • 18
  • 26
  • what is *the simple way* from your point of view? – Rafael Mar 02 '20 at 08:51
  • If you don't use any library, you would still end up implementing probably the same algorithm used by library, which doesn't make sense unless you want to learn the algorithm itself. So, again, what do you mean by simple here ? – adzo261 Mar 02 '20 at 08:54
  • The question doesn't appear to include any attempt at all to solve the problem. Please edit the question to show what you've tried, and show a specific roadblock you're running into with [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask). – Andreas Mar 02 '20 at 08:54
  • 1
    See my proof using backtrack [here](https://stackoverflow.com/a/58314302/10910098). – bathudaide Mar 02 '20 at 08:56
  • @ adzo261 I meant 'a straightforward way to do it' – studenthahahoho Mar 02 '20 at 09:00
  • *"Is there any way to do random permutation without using any in-built library?"* Yes, that means you need a shuffling algorithm. *"Shuffling looks so complicated to me."* I see, so you need to do X but you don't want to do X. It's a direct contradiction. – kaya3 Mar 02 '20 at 09:03
  • 1
    "I know how to generate random numbers using Math.random()" - that _is_ using an in-built library already. – daniu Mar 02 '20 at 09:15
  • @ daniu I meant to say that I didn't want to import something like this import java.util – studenthahahoho Mar 02 '20 at 09:58
  • 1
    What's wrong with importing? I'm not trying to be annoying, I'm just curious. – NomadMaker Mar 02 '20 at 10:16
  • 1
    @studenthahahoho java.util is a standard java library, it is not an external library. It sounds like you don't want to use java's features. To me, this sounds like saying "how can I get the rain off my car's windshield, without using the car's wipers?" – Bentaye Mar 02 '20 at 10:30

2 Answers2

1

Could you do something like the example shown below

public class Main {
    public static void main(String[] args) throws Exception {
        Integer[] input = new Integer[] { 1, 2, 3 };
        Integer[] shuffleInput = shuffle(input);

        for (int i=0; i<shuffleInput.length; i++) {
            System.out.print(shuffleInput[i]);
        }
    }

    public static Integer[] shuffle(Integer[] input) {
        Integer[] inputCopy = input.clone();;
        Integer[] output = new Integer[input.length];

        for (int i=0; i<output.length; i++) {
            // Find and get a random element
            int randPicker = (int)(System.currentTimeMillis() % inputCopy.length);
            output[i] = inputCopy[randPicker];

            // Remove selected element from copy
            Integer[] aux = new Integer[inputCopy.length - 1];
            System.arraycopy(inputCopy, 0, aux, 0, randPicker);
            if (inputCopy.length != randPicker) {
                System.arraycopy(inputCopy, randPicker + 1, aux, randPicker, inputCopy.length - randPicker - 1);
            }
            inputCopy = aux;
        }

        return output;
    }
}

This code takes a list of Integer of any size as input, and return a list with the mixed values.

If the list will always be 3 numbers, it could be a simpler version.

EDITED: This version does not use Math.random() or any other java.util.

John
  • 770
  • 1
  • 9
  • 18
0

One algorithm for sorting an array is based on selection sort and is called selection shuffle. Go through the array and for each index, pick a random index following it and swap those two indices. Since this looks a bit like homework, I will refrain from giving any actual code, but this is a solution.

guninvalid
  • 411
  • 2
  • 5
  • 16