52张牌,分牌不一定是从正中间分,范围是25-30,落牌张数为1-3张
package Puke;
import java.util.Random;
public class Neww {
public static void main(String[] args) {
// TODO Auto-generated method stub
int cards[]=new int[52];
for(int i=0;i<52;i++) {
cards[i]= i+1; //设置数组
}
Random rand = new Random();
int n = rand.nextInt();
n = rand.nextInt(6)+25; //n为25-30的随机数
int shuffled[]=new int[52]; //设置新数组shuffled[]
int postl[]=new int[n]; //设置左边所有数组成的数组为postl[]
int postr[]=new int[52-n]; //设置右边所有数组成的数组为postr[]
System.out.println("n="+n); //输出n
for(int i=0;i<n;i++)
{
postl[i]=cards[i]; //把n左边的数字都放入postl[]数组中
}
for(int i=0;i<52-n;i++)
{
postr[i]=cards[i+n]; //把n右边的数字都放入postr[]数组中
}
int sum1=0,sum2=0;
int count=51;
while((n-sum1!=0)&&(52-n-sum2!=0)) //当左边或右边的数组还有数字时
{
int m1 = rand.nextInt(); //m1为1-3的随机数
m1= rand.nextInt(3)+1;
int m2 = rand.nextInt(); //m2为1-3的随机数
m2 = rand.nextInt(3)+1;
if((n-sum1>=m1)&&(52-n-sum2>=m2)) //当左边或右边的数组中的长度大于随机数
{
for(int step=0;step<m1;step++)
{
shuffled[count]=cards[postl[n-sum1-1]-1];
//左边按每次随机数m1次数循环输出
count=count-1; //剩下的牌数
sum1=sum1+1;
}
for(int step1=0;step1<m2;step1++)
{
shuffled[count]=cards[postr[52-n-sum2-1]-1];
//右边按随机数m2次数循环输出
count=count-1;
sum2=sum2+1;
}
}
if(n-sum1==0) //如果刚刚好左边的数输出完
{
for(int step=0;step<52-n-sum2;step++)
{
shuffled[step]=cards[postr[step]-1]; //右边剩下的数全输出
}
}
else if(52-n-sum2==0) //如果刚刚好右边的数输出完
{
for(int step=0;step<n-sum1;step++)
{
shuffled[step]=cards[postl[step]-1]; //左边剩下的数全输出
}
}
}
System.out.println();
for(int step=0;step<52;step++)
{
System.out.print(shuffled[step]+" "); //输出洗完牌的结果
}
}
}