random随机数

短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个。

本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62操作,结果作为索引取出字符,

这样重复率大大降低。

经测试,在生成一千万个数据也没有出现重复,完全满足大部分需求。

代码贴出来供大家参考。

public static String[] chars = new String[] { "a""b""c""d""e""f",
            "g""h""i""j""k""l""m""n""o""p""q""r""s",
            "t""u""v""w""x""y""z""0""1""2""3""4""5",
            "6""7""8""9""A""B""C""D""E""F""G""H""I",
            "J""K""L""M""N""O""P""Q""R""S""T""U""V",
            "W""X""Y""Z" };
 
 
public static String generateShortUuid() {
    StringBuffer shortBuffer = new StringBuffer();
    String uuid = UUID.randomUUID().toString().replace("-""");
    for (int i = 0; i < 8; i++) {
        String str = uuid.substring(i * 4, i * 4 4);
        int x = Integer.parseInt(str, 16);
        shortBuffer.append(chars[x % 0x3E]);
    }
    return shortBuffer.toString();
 
}

http://my.oschina.net/vvcumt/blog/491504

##########################################

java中生成不重复随机的数字  

2010-04-26 22:24:00|  分类: JAVA程序|举报|字号 订阅

 
 

Java中产生随机数

1 . 调用java.lang下面Math类中的random()方法产生随机数

        新建一个文件后缀名为java的文件,文件名取为MyRandom,该类中编写如下的代码:

          public class MyRandom {

                  public static void main(String[] args) {

                          int  radom = (int)(Math.random()*10);

                          System.out.println(radom); 

                  }

          }

        其中Math.random() //产生0~1之间的一个随机小数。

        产生一个0~9之间的整数为:(int)(Math.random()*10);

        产生一个1~10之间的整数则可以写着:(int)(Math.random()*10 + 1);

        以此类推:产生一个0~n之间的数就应写作:Math.random()*n;

       取出一个指定长度大小的随机正整数:

       public static int buildRandom(int length) {
            int num = 1;
            double random = Math.random();
            if (random < 0.1) {
                 random = random + 0.1;
            } for (int i = 0; i < length; i++) {
                 num = num * 10;
            }
            return (int) ((random * num));
     }

2 . 调用java.util下面Random类,此类的实例用于生成伪随机数流,产生一个随机整数,则调用该类的nextInt()方法

      使用Random类之前,则在包下导入java.util.Random ;代码为:

import java.util.Random;

public class MyRandom {

     public static void main(String[] args) {

         Random rand = new Random();

        int rInt = rand.nextInt(10);

       System.out.println(rInt);

    }

}

其中Random rand = new Random()是创建一个新随机数生成器;rand.nextInt(int n)是从此随机数生成器的序列中取出的、在 0(包括)和指定值n(不包括)之间均匀分布的 int值。

Java中在指定的整数范围类,循环产生不相同的随机数

以产生6位20以内不相同的随即整数 为例如下:

public class MyRandom {

      public static void main(String[] args) {

           int n = 20;

          Random rand = new Random();

          boolean[]  bool = new boolean[n];

          int randInt = 0;

          for(int i = 0; i < 6 ; i++) {

               do {

                   randInt  = rand.nextInt(n);

               }while(bool[randInt]);

              bool[randInt] = true;

              System.out.println(randInt);

         }

    }

}

其中用布尔变量数组来存储是否生成了该数字。生成后,该数字作为布尔数组下表的对应布尔值变成了true,下一次生成该数字就会再次进入do...while循环生成数字直到产生没有生成过的数字。

例如:生成一个int类型的数组,长度为50的,并向其中随即插入0-50之间的数,并且不能重复。

代码如下:

public class MyRandom {

      public static void main(String[] args) {

        int[] intRandom = new int[50];
        List mylist = new ArrayList();  //生成数据集,用来保存随即生成数,并用于判断
        Random rd = new Random();
        while(mylist.size() < 50) {
            int num = rd.nextInt(51);

           /**用集合的contains方法,来判断该数据集中是否包含随即数num,

            * 如果含有返回true。不包含就是false。!表示“非”。

            *!mylist.contains(num))是个布尔值,只有当该值为true时才执行其内部的操作,即为不包含时才执行。

          */
            if(!mylist.contains(num)) {  
               mylist.add(num);  //往集合里面添加数据。
            }
        }

       /**给数值赋值*/
       for(int i = 0;i <mylist.size();i++) {
            intRandom[i] = (Integer)(mylist.get(i));
       }

    }

}

 
 
 
 
阅读(22960)评论(2)
http://gongyanghui1986.blog.163.com/blog/static/137485319201032610240896/
http://developer.51cto.com/art/200906/128348.htm
 
##############################################################

Java生成随机数的2种示例方法代码

作者: 字体:[增加 减小] 类型:转载 时间:2013-11-25 我要评论

在Java中,生成随机数有两种方法。1是使用Random类。2是使用Math类中的random方法。看下面的例子使用吧

我们现在做个例子,比如生成20个0到10之间的随机数。

1.使用Random类的nextInt(n)方法,n代表0到n之间,包括0,不包括n

复制代码代码如下:

Random random = new Random();
for(int i=0;i<20;i++)
{
  System.out.println(random.nextInt(10));
}

2.使用Math类中的random方法,它生成的随机数是0.0到1.0之间的double。要生成int就需要类型转换

复制代码代码如下:

for(int i=0;i<10;i++)
{
    double n = Math.random();
    n *= 10;
    int m = (int)n;
    System.out.println(m);
}
 http://www.jb51.net/article/43694.htm
 
 
###################################################

Java产生不重复随机数方法

2009-06-11 15:16 孙丰伟 百度博客 字号:T | T
一键收藏,随时查看,分享好友!

本文讲述了Java生成不重复随机数的两种方法,并给出了其实现的相应的代码。

AD:51CTO网+ 首届中国APP创新评选大赛火热招募中……

关于生成Java不重复的随机数:

  1. import java.util.*;  
  2. public class Test...{  
  3.     public static void main(String[] args)...{  
  4.         //生成 [0-n) 个不重复的随机数  
  5.         / st 用来保存这些随机数  
  6.         ArrayList list = new ArrayList();  
  7.           
  8.           
  9.         int n = 10;  
  10.         Random rand = new Random();  
  11.         boolean[] bool = new boolean[n];  
  12.           
  13.         int num =0;  
  14.           
  15.         for (int i = 0; i<n; i++)...{  
  16.               
  17.       
  18.             do...{  
  19.                 //如果产生的数相同继续循环  
  20.                 num = rand.nextInt(n);      
  21.                
  22.             }while(bool[num]);  
  23.               
  24.             bool[num] =true;  
  25.               
  26.             list.add(num);  
  27.           
  28.           
  29.         }  
  30.               
  31.       
  32.         System.out.println (list);      
  33. }          
  34.           
  35.  
  36.  
  37. public class Test  
  38. {  
  39.  
  40.     public static void main(String[] args)  
  41.     {  
  42.         int[] arr = new int[10];  
  43.  
  44.         for (int i = 0; i < 10; i++)  
  45.         {  
  46.             arr[i] = (int) (Math.random() * 40) + 1;  
  47.             for (int j = 0; j < i; j++)  
  48.             {  
  49.                 if (arr[j] == arr[i])  
  50.                 {  
  51.                     i--;  
  52.                     break;  
  53.                 }  
  54.             }  
  55.         }  
  56.         for (int i = 0; i < 10; i++)  
  57.             System.out.print(arr[i] + " ");  
  58.     }  
  59. }  
  60.  
  61.  
  62. b.  
  63.  
  64. Java code  
  65.  
  66.  
  67. import   java.util.*;   
  68. public   class   Test   
  69. {   
  70.           
  71.         public   static   void   main(String[]   args)   
  72.         {   
  73.                 int   n=40;   
  74.                 int[]   num   =   new   int[n];   
  75.                 for(int   i=0;i <num.length;i++)   
  76.                         num[i]   =   i+1;   
  77.                 int[]   arr   =   new   int[10];   
  78.                 for(int   i=0;i <arr.length;i++)   
  79.                 {   
  80.                         int   r   =(int)(Math.random()*n);   
  81.                         arr[i]=num[r];   
  82.                         num[r]=num[n-1];   
  83.                         n--;   
  84.                 }   
  85.                 for(int   i=0;i <arr.length;i++)   
  86.                         System.out.print(arr[i]+"   ");   
  87.         }   
  88. }  
  89.  
  90.  
  91.  
  92. c.  
  93.  
  94. Java code  
  95.  
  96.  
  97. import   java.util.*;   
  98. public   class   Test   
  99. {   
  100.           
  101.         public   static   void   main(String[]   args)   
  102.         {   
  103.                 LinkedList <Integer>   myList=   new   LinkedList <Integer> ();   
  104.                 int   n=40;   
  105.                 for(int   i=0;i <n;i++)   
  106.                         myList.add(i+1);   
  107.                 int[]   arr   =   new   int[10];   
  108.                 for(int   i=0;i <arr.length;i++)   
  109.                 {   
  110.                         arr[i]=myList.remove((int)(Math.random()*n));   
  111.                         n--;   
  112.                 }   
  113.                 for(int   i=0;i <arr.length;i++)   
  114.                 {   
  115.                         System.out.print(arr[i]+"   ");   
  116.                 }   
  117.         }   
  118. }  
  119.  
  120.  
  121.  
  122. d.  
  123.  
  124. Java code  
  125.  
  126.  
  127. import   java.util.*;   
  128. public   class   Test   
  129. {   
  130.           
  131.         public   static   void   main(String[]   args)   
  132.         {   
  133.                 Set <Integer>   mySet   =   new   LinkedHashSet <Integer> ();   
  134.                 while(mySet.size() <10)   
  135.                 {   
  136.                         mySet.add((int)(Math.random()*40+1));   
  137.                 }   
  138.                 for(Integer   i:mySet)   
  139.                 {   
  140.                         System.out.print(i+"   ");   
  141.                 }   
  142.         }   
  143. }  
  144.  


方法一:
在一个待选数组中随机产生一个数,然后把他放到待选数组的最后,然后从length-1里随机产生下一个随机数,如此类推

  1. public static int[] randoms()  
  2. {  
  3. Random r = new Random();  
  4.  
  5. int temp1,temp2;  
  6. int send[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};  
  7. int len = send.length;  
  8. int returnValue[] = new int[22];  
  9. for(int i=0;i<22;i++)  
  10. {  
  11. temp1 = Math.abs(r.nextInt())% len;  
  12. returnValue[i] = send[temp1];  
  13. temp2 = send[temp1];  
  14. send[temp1] = send[len-1];  
  15. send[len-1] = temp2;  
  16. len--;  
  17. }  
  18. return returnValue;  
  19. }  

方法二:
还是一个固定的无重复的数组,然后把这个数组随机调换位置,多次之后这个数组就是一个无重复的随机数组了。

  1. public static int[] random2()  
  2. {  
  3.    int send[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};  
  4.    int temp1,temp2,temp3;  
  5.    Random r = new Random();  
  6.    for(int i=0;i<send.length;i++) //随机交换send.length次  
  7.    {  
  8.     temp1 = Math.abs(r.nextInt())%(send.length-1); //随机产生一个位置  
  9.     temp2 = Math.abs(r.nextInt())%(send.length-1); //随机产生另一个位置  
  10.     if(temp1 != temp2)  
  11.     {  
  12.      temp3 = send[temp1];  
  13.      send[temp1] = send[temp2];  
  14.      send[temp2] = temp3;  
  15.     }  
  16.    }  
  17.    return send;  
  18. }  
    1. http://developer.51cto.com/art/200906/128348.htm
原文地址:https://www.cnblogs.com/pengmn/p/5254656.html