如何在java中生成随机排列

时间:2014-02-21 05:28:22

标签: java random

我为我的班级编写一个小型益智游戏,我不明白如何做这个部分,它说

int [] toArray():在数组中返回此排列,其中第一行的元素首先出现,然后是第二行的元素,等等。

shuffle():返回一个保证可以解决的排列。方法很简单。从身份置换开始,生成大量随机生成的移动。想象一下,不是移动瓷砖,而是移动空白区域。在我的实现中,我正在应用1000个随机移动。

如何做toArray方法和shuffle方法

到目前为止,我有这个

public class RandomPermutation {

  private int row=3;
  private column=3;

  RandomPermutation(int row, int column){
    this.row=row;
    this.colmn=column;
    board
  }

  private int[] toArray(){

2 个答案:

答案 0 :(得分:0)

我按原样提供此代码段,不附带任何明示或暗示的担保,包括但不限于适销性和适用于特定用途的暗示担保,例如暗中获取完整分数你的任务。

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SlidingPuzzleRandomPermutation
{
    public static void main(String[] args)
    {
        SlidingPuzzleRandomPermutation p =
            new SlidingPuzzleRandomPermutation(4,3);
        System.out.println(p);
        p.shuffle();
        System.out.println(p);
    }

    private final Random random = new Random(0);
    private final int row;
    private final int column;
    private final int array[];

    SlidingPuzzleRandomPermutation(int row, int column)
    {
        this.row = row;
        this.column = column;
        this.array = new int[row * column];
        for (int i=0; i<array.length; i++)
        {
            array[i] = (i+1)%array.length;
        }
    }

    int[] toArray()
    {
        return array.clone();
    }

    void shuffle()
    {
        final int UP = 0;
        final int DOWN = 1;
        final int LEFT = 2;
        final int RIGHT = 3;
        int zeroR = row-1;
        int zeroC = column-1;
        for (int i=0; i<1000; i++)
        {
            List<Integer> validDirections = new ArrayList<Integer>();
            if (zeroR > 0) validDirections.add(UP);
            if (zeroR < row-1) validDirections.add(DOWN);
            if (zeroC > 0) validDirections.add(LEFT);
            if (zeroC < column-1) validDirections.add(RIGHT);
            int directionIndex = random.nextInt(validDirections.size());
            int direction = validDirections.get(directionIndex);
            int nextR = zeroR;
            int nextC = zeroC;
            switch (direction)
            {
                case UP   : nextR--; break;
                case DOWN : nextR++; break;
                case LEFT : nextC--; break;
                case RIGHT: nextC++; break;
            }
            swap(zeroR, zeroC, nextR, nextC);

            zeroR = nextR;
            zeroC = nextC;
        }

        while (zeroR < row-1)
        {
            swap(zeroR, zeroC, zeroR+1, zeroC);
            zeroR++;
        }
        while (zeroC < column-1)
        {
            swap(zeroR, zeroC, zeroR, zeroC+1);
            zeroC++;
        }
    }

    private void swap(int r0, int c0, int r1, int c1)
    {
        int index0 = c0 + r0 * column;
        int index1 = c1 + r1 * column;
        int temp = array[index0];
        array[index0] = array[index1];
        array[index1] = temp;

        //System.out.println("After swapping "+r0+" "+c0+" and "+r1+" "+c1);
        //System.out.println(this);
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        for (int r=0; r<row; r++)
        {
            for (int c=0; c<column; c++)
            {
                int index = c + r * column;
                sb.append("["+array[index]+"]");
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}

(我还在练习。当我长大后,我想成为一名程序员)

答案 1 :(得分:0)

以下代码可用于为给定的一组整数生成随机排列。

    // random object
    Random rnd = new Random();

    // the entry set
    ArrayList<Integer> entry_set = new ArrayList<Integer>();

    // add element to the entry set
    for (int i=0; i<5; i++) {
        entry_set.add(i);
    }
    System.out.println("Entry set: " + entry_set);

    // a random permutation
    ArrayList<Integer> pi_per = new ArrayList<Integer>();

    // entry set size
    int s = entry_set.size();

    while (s > 0) {

        // get a random index between 0 - s
        int indx = rnd.nextInt(s);

        // get the number from the entry set at this index
        int num = entry_set.get(indx);

        // delete this number from the entry set
        entry_set.remove(indx);

        // add this number to the permutation set
        pi_per.add(num);

        // update s
        s = entry_set.size();
    }

    // print
    System.out.println("Random Permutation: " + pi_per);