Java如何获取随机数

1.使用org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(10)取数字字母随机10位; //取得一个3位随机数字字符串 String num = RandomStringUtils.random(3, false, true); //取得一个3位的随机字母,并将字母转化为大写字母 String str = Random

1.使用org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(10)取数字字母随机10位;
    //取得一个3位随机数字字符串 
    String num = RandomStringUtils.random(3, false, true);       
    //取得一个3位的随机字母,并将字母转化为大写字母
    String str = RandomStringUtils.random(3, true, false);
    //生成长度为count的随机ASCII字串
    RandomStringUtils.randomAscii(int count)   
    //生成长度为count的随机字母字串
    RandomStringUtils.randomAlphabetic(int count)
2.使用Random rad = new Random();
    int radnum = rad.nextInt(10);//取小于10的正整数
3.取范围内的随机数:
    Math.round(Math.random() * (255 - 1) + 1)最大255,最小1
    int a = (int)(Math.random()*1000)//产生0-1000的整数随机数


Java代码   收藏代码
  1. import java.util.Random;  
  2.   
  3. import org.apache.commons.lang3.RandomStringUtils;  
  4.   
  5. public class RandomStringUtilsDemo {  
  6.       
  7.     private static final Random RANDOM = new Random();  
  8.       
  9.     private static final int count = 4;  
  10.       
  11.     private static final int start = 0;  
  12.       
  13.     private static final int end = 3;  
  14.       
  15.     private static final String str = "abcd1234";  
  16.       
  17.     private static final char[] chars = { 'a''b''1''2''c''d''e',  
  18.             'f''3' };  
  19.   
  20.     public static void main(String[] args) {  
  21.         /* 
  22.          * 1.创建一个长度为count的随机字符串. 
  23.          * 中文环境下是乱码. 
  24.          */  
  25.         System.out.println("random1:" + RandomStringUtils.random(count));  
  26.   
  27.         /* 
  28.          * 2.创建一个长度为count的从指定字符数组chars组合成的随机字符串. 
  29.          */  
  30.         System.out.println("random2:" + RandomStringUtils.random(count, chars));  
  31.   
  32.         /* 
  33.          * 3.创建一个长度为count的从指定字符串str抽取字符组合成的随机字符串 
  34.          */  
  35.         System.out.println("random3:" + RandomStringUtils.random(count, str));  
  36.   
  37.         /* 
  38.          * 4.根据letters和numbers 创建一个长度为count的随机字符串. letters 为true 返回的值包含字母字符 
  39.          * numbers 为true 返回的值包含数字字符 
  40.          */  
  41.         System.out.println("random4:"  
  42.                 + RandomStringUtils.random(count, truetrue));  
  43.   
  44.         /* 
  45.          * <h1>此方法有问题 无法执行</h1>  
  46.          *  
  47.          * 5.根据start,end,letters和numbers 创建一个长度为count的随机字符串. 
  48.          * start 参数字符组开始的位置 end 参数字符组结束的位置 letters 为true 返回的值包含字母字符 numbers 
  49.          * 为true 返回的值包含数字字符 
  50.          */  
  51.         // System.out.println("random5:"+RandomStringUtils.random(count, start, end, true, true));  
  52.   
  53.         /* 
  54.          * 6.根据start,end,letters和numbers, 创建一个长度为count从指定字符数组chars抽取字符的随机字符串. 
  55.          * start 参数字符组开始的位置 end 参数字符组结束的位置 letters 为true 返回的值包含字母字符 numbers 
  56.          * 为true 返回的值包含数字字符 chars 字符数组 
  57.          */  
  58.         System.out.println("random6:"  
  59.                 + RandomStringUtils  
  60.                         .random(count, start, end, truetrue, chars));  
  61.   
  62.         /* 
  63.          * 7.根据start,end,letters和numbers, 创建一个长度为count从一个随机数源抽取字符的随机字符串. start 
  64.          * 参数字符组开始的位置 end 参数字符组结束的位置 letters 为true 返回的值包含字母字符 numbers 为true 
  65.          * 返回的值包含数字字符 chars 字符数组 RANDOM 随机数源 
  66.          */  
  67.         System.out.println("random7:"  
  68.                 + RandomStringUtils.random(count, start, end, truetrue,  
  69.                         chars, RANDOM));  
  70.   
  71.         /* 
  72.          * 8.创建一个长度为count的字母字符随机字符串 
  73.          */  
  74.         System.out.println("randomAlphabetic:"  
  75.                 + RandomStringUtils.randomAlphabetic(count));  
  76.   
  77.         /* 
  78.          * 9.创建一个长度为count的包含字符字符和数字字符的随机字符串 
  79.          */  
  80.         System.out.println("randomAlphanumeric:"  
  81.                 + RandomStringUtils.randomAlphanumeric(count));  
  82.   
  83.         /* 
  84.          * 10.创建一个长度为count的从ASCII 32到126 组成的随机字符串 
  85.          */  
  86.         System.out.println("randomAscii:"  
  87.                 + RandomStringUtils.randomAscii(count));  
  88.   
  89.         /* 
  90.          * 11.创建一个长度为count的数字字符随机字符串 
  91.          */  
  92.         System.out.println("randomNumeric:"  
  93.                 + RandomStringUtils.randomNumeric(count));  
  94.   
  95.     }  
  96.   
  97. }  

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值