当前位置: 代码迷 >> Java相关 >> 产生随机数组的算法
  详细解决方案

产生随机数组的算法

热度:300   发布时间:2013-05-22 15:14:02.0
产生随机数组的算法

说明:以下是我在看到随机验证码产生后自己随手写出来的一个算法,代码有点乱而且应该还有问题,请各位指出好让我多多改进以下,谢谢!!
/*
*Time:2013/5/21
*finish Time:2013/5/22
*author:****
*功能:产生一个长度为leng的随机数组且
*      可以设置某个数值在gap间隔内不重复出现
*说明:RandomArray成员函数返回的结果是String类型 如果想转换为int型
       可以用该方法
       MankRandom mr = new MankRandom();
       String str  = mr.RandomArray(12,5);
       for(int i = 0 ;i < 13 ;i++)
      {
         int index = Integer.parseInt(String.valueOf(str.charAt(i-1)));
          >>>>>>>>other code
       }
*/



class  MankRandom
{
    //源数据
    private String source="1,2,3,4,5,6";
    //数据容器
    private String [] str;
   
    //起始标志
    private int flag;
   
    //结果集
    private StringBuilder sb  = null;
   
    //构造函数
    public MankRandom()
    {//分割源数据数组 将分割后的元素存放在string数组中
        str = source.split(",");
        //初始化起始标志
        this.flag = -1;
        //给结果集添加元素
        sb = new StringBuilder("");
    }
   
    //随机数组产生函数
    public String RandomArray(int length ,int gap)
    {
        for(int i = 0 ;i<length;i++)
        {
            //如果是起始元素
            if(this.flag == -1)
            {
                //获取一个在【0,6】之间的随机数
                Random ran = new Random();
                int k = ran.nextInt(7);
                //改变起始标志
                this.flag = k;
                //赋值
                this.sb.replace(0, 1,String.valueOf(k));
            }else if(this.flag!=-1 && sb.length()<gap)
            {
                //如果不是起始赋值而且结果集的长度达不到间隔长度gap
                boolean isCircle = true;
                Random ran = new Random();
                int k =0;
                while(isCircle)
                {
                    isCircle = false;
                    //获取一个在【0,6】之间的随机数               
                    k = ran.nextInt(7);
                    //遍历整个StringBuilder字符串中的字符
                    for(int m = 0 ;m<this.sb.length();m++)
                    {
                        //如果新产生的随机数在StringBuilder字符串中 则直接跳出for循环
                        //进入while循环重新生成新的随机数
                        if(String.valueOf(k).equals(String.valueOf(this.sb.charAt(m))))
                        {
                            isCircle = true;
                            break;
                        }
                    }
                    
                    if(isCircle == false)
                    {
                    //    赋值
                        this.sb.append(k);
                        break;
                    }
                    
//                    isCircle = false;
//                        
                }        
            }
            else if(this.flag!=-1 && this.sb.length()>=gap)
            {
                //如果不是起始赋值而且结果集的长度大于间隔长度gap
                //此时要截取sb字符串中的后面gap个字符
                //1.先生成一个新的随机数
                Random ran =new Random();
                int k = 0;
                boolean isCircle = true;
               
                while(isCircle)
                {
                    isCircle = false;
                    //获取一个在【0,6】之间的随机数            
                    k = ran.nextInt(7);
                    //判断在 [this.sb.length()-1-gap,this.sb.length()]之间
                    //是否存在k这个字符
                    for(int m = this.sb.length()-gap;m<this.sb.length();m++)
                    {
                        if(String.valueOf(k).equals(String.valueOf(sb.charAt(m))))
                        {
                            isCircle = true;
                            break;
                        }
                    }
                    
                    if(isCircle == false)
                    {
                        //赋值
                        this.sb.append(k);
                        break;
                    }
                }
                    
            }
        }
        //输出最后结果
        return this.sb.toString();
    }
   
}
搜索更多相关的解决方案: 验证码  而且  

----------------解决方案--------------------------------------------------------
  相关解决方案