package org.guocai.entity;
import java.util.Random;
public class test2 {
public static void main(String[] args) {
// 1.substring测试
String id="12345432123";
String shortId,longId,Id;
shortId=id.substring(2,7);//要索引从2到7
longId=id.substring(1,8);//要索引从1到8
Id=id.substring(4);//第4个开始往后
//System.out.println(shortId);
//System.out.println(longId);
//System.out.println(Id);
//2.获取十六进制的颜色代码.例如 "#6E36B4" , For HTML
String r,g,b;
Random random = new Random();
r = Integer.toHexString(random.nextInt(256)).toUpperCase();
g = Integer.toHexString(random.nextInt(256)).toUpperCase();
b = Integer.toHexString(random.nextInt(256)).toUpperCase();
r = r.length()==1 ? "0" + r : r ;
g = g.length()==1 ? "0" + g : g ;
b = b.length()==1 ? "0" + b : b ;
//System.out.println(r+g+b);
//3.产生一个100以内的随机整数
int x=(int)(Math.random()*1000);
//System.out.println(x);
//4.产生一个1-50之间的随机数:
int x2=1+(int)(Math.random()*50);
//System.out.println(x2);
//5.产生一个0到z-1的正数
int z = 1000;
Random random2 = new Random();
int x3 = random2.nextInt(z);
//System.out.println(x3);
//6.产生浮点数有Random类的nextFloat方法,总之nextXXX方法是用来产生随机数的。
Double x4 = (double) random2.nextFloat();
//System.out.println(x4);
Random random3 = new Random(490);//指定种子数字
int num = random3.nextInt(500);
//System.out.println(num);
Random rand=new Random();
int i=(int)(Math.random()*100); // 生成0-100的随机数
int j=rand.nextInt(100); // 这里是一个方法的重载,参数的内容是指定范围
//System.out.println("i:"+i+"\nj:"+j); // 分别输出两个随机数
test2 rt = new test2();
rt.testRandom();
}
public void testRandom(){
System.out.println("Random不设置种子:");
for (int i = 0; i < 5; i++) {
Random random = new Random();
for (int j = 0; j < 10; j++) {
System.out.print(" " + random.nextInt(100) + ", ");
}
System.out.println("");
}
System.out.println("");
System.out.println("Random设置种子:");
for (int i = 0; i < 5; i++) {
Random random = new Random();
random.setSeed(100);
for (int j = 0; j < 10; j++) {
System.out.print(" " + random.nextInt(100) + ", ");
}
System.out.println("");
}
}
}