Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Random;

public class Main {
    private static final Random generator = new Random();

    public static String getRandomPermutation(String s) {
        StringBuffer buffer = new StringBuffer(s);
        int n = s.length();
        for (int i = 0; i < n; i++) {
            int random = generator.nextInt(n);
            buffer = swap(buffer, i, random);
        }
        return new String(buffer);
    }

    private static StringBuffer swap(StringBuffer s, int a, int b) {
        char t = s.charAt(a);
        s.setCharAt(a, s.charAt(b));
        s.setCharAt(b, t);
        return s;
    }
}