java数组随机乱序且乱序后不在其原来的位置上(代码片段)

来源:互联网 发布:海淘网站farfetch 知乎 编辑:程序博客网 时间:2024/06/06 09:49

背景:因业务需要,需要对一个数组进行乱序,如:A、B、C、D相互交换顺序,交换顺序后要求每个人都不能拿到自己的编号,交换后:DCBA、DABC等符合要求,而ADCB、DBCA这类则不符合要求,背景说明完毕,上代码:

package com.sdzn.util;import java.util.ArrayList;import java.util.List;public class RandomUtil {    /**     * main方法描述: 随机数工具     *      * @author : Ricky     * @createTime : Jun 19, 2015 1:16:54 PM     * @param args     */    public static void main(String[] args) {        for (int k = 0; k < 10000; k++) {            int[] sort = {                    1, 2, 3, 4, 5, 6, 7, 8, 9, 10            };            int[] newss = new RandomUtil().randomSort(sort);            for (int p = 0; p < sort.length; p++) {                if (sort[p] == newss[p]) {                    System.out.println("相等了");                }            }            if (k % 100 == 0) {                System.out.println(k);                for (int p = 0; p < sort.length; p++) {                    System.out.print(newss[p] + " ");                }            }        }    }    /**     * randomSort方法描述: 将一个数组进行随机排序,使每个序列位置上的数字都不是原来的数字     * 使用场景:****。     *      * @author : Ricky     * @createTime : Jun 19, 2015 3:38:55 PM     * @param sort     *            需要打乱顺序的数组     * @return     */    public int[] randomSort(int[] sort) {        List<Integer> sList = new ArrayList<Integer>();        List<Integer> rList = new ArrayList<Integer>();        int[] rSort = new int[sort.length];        for (int s : sort) {            sList.add(s);        }        int count = 0;        Integer lastValue = null;        while (sList.size() > 0) {            int randSeq = getRandom(sList.size());            if (sList.get(randSeq) != sort[count]) {                rList.add(sList.get(randSeq));                sList.remove(randSeq);                count++;            } else {                if (count + 1 == sort.length) {                    if (sList.get(randSeq) == sort[count]) {                        lastValue = sort[count];                        rList.add(sList.get(randSeq));                        sList.remove(randSeq);                    }                    break;                }            }        }        for (int i = 0; i < rList.size(); i++) {            rSort[i] = rList.get(i);        }        if (lastValue != null) {            int t = rSort[sort.length - 1];            int randomSeq = getRandom(sort.length - 1);            rSort[sort.length - 1] = rSort[randomSeq];            rSort[randomSeq] = t;        }        return rSort;    }    /**     * getRandom方法描述: 获得小于quan的一个整数     *      * @author : Ricky     * @createTime : Jun 19, 2015 1:44:19 PM     * @param quan     *            产生的整形数字必须比它小     * @return     */    private int getRandom(int quan) {        int newInt = quan;        while (!(newInt < quan)) {            String randomStr = "" + quan * Math.random();            if (randomStr.contains("."))                randomStr = randomStr.substring(0, randomStr.indexOf("."));            newInt = Integer.parseInt(randomStr);        }        return newInt;    }}
0 0
原创粉丝点击