2019.2.26实用类随机数作业

1.

 1 package com.demo1;
 2 
 3 import java.util.Random;
 4 
 5 public class Test2 {
 6     public static void main(String[] args) {
 7         Random ran = new Random();
 8         int index = 1;
 9         do {
10             int a = ran.nextInt(7)+3;//随机生成0~7之间数字然后加3
11             int b = ran.nextInt(1000000000);
12             do {
13                 b = ran.nextInt(1000000000);
14             }while(b<100000000);
15             System.out.println("1"+a+b);
16             index++;
17         }while(index<10);
18     }
19 }

输出:

2.

 1 public static String getCode(int length) {
 2         String val = "";
 3         Random random = new Random();
 4         for (int i = 0; i < length; i++) {
 5             String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
 6             // 输出字母还是数字
 7             if ("char".equalsIgnoreCase(charOrNum)) {
 8                 // 输出是大写字母还是小写字母
 9                 int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
10                 val += (char) (random.nextInt(26) + temp);
11             } else if ("num".equalsIgnoreCase(charOrNum)) {
12                 val += String.valueOf(random.nextInt(10));
13             }
14         }
15         return val;
16     }
 1 package com.demo1;
 2 
 3 import java.util.Random;
 4 
 5 public class Test3 {
 6     public static void main(String[] args) {
 7         Random ran = new Random();
 8         //定义一个数字num
 9         char num;
10         //循环6次得到6个
11         for(int i = 0;i<6;i++) {
12             while(true) {
13                 num = (char)ran.nextInt();//强制转换为char类型
14                 //根据ASC码表定义取值范围
15                 if(num>47&&num<58||num>64&&num<91||num>96&&num<123) {
16                     break;
17                 }
18             }
19             System.out.print(num);
20         }
21 
22     }
23 }

输出:

原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10439268.html