final long l = System.currentTimeMillis();
final long l = System.currentTimeMillis(); final int i = (int)( l % 100 );
final double d = Math.random();
final double d = Math.random(); final int i = (int)(d*100);
Random random = new Random();//默认构造方法 Random random = new Random(1000);//指定种子数字
int i2 = random.nextInt(100);
// 构造函数(一): 创建一个新的随机数生成器。
Random()
// 构造函数(二): 使用单个 long 种子创建一个新随机数生成器: public Random(long seed) { setSeed(seed); } next 方法使用它来保存随机数生成器的状态。
Random(long seed)
boolean nextBoolean() // 返回下一个“boolean类型”伪随机数。
void nextBytes(byte[] buf) // 生成随机字节并将其置于字节数组buf中。
double nextDouble() // 返回一个“[0.0, 1.0) 之间的double类型”的随机数。
float nextFloat() // 返回一个“[0.0, 1.0) 之间的float类型”的随机数。
int nextInt() // 返回下一个“int类型”随机数。
int nextInt(int n) // 返回一个“[0, n) 之间的int类型”的随机数。
long nextLong() // 返回下一个“long类型”随机数。
synchronized double nextGaussian() // 返回下一个“double类型”的随机数,它是呈高斯(“正常地”)分布的 double 值,其平均值是 0.0,标准偏差是 1.0。
synchronized void setSeed(long seed) // 使用单个 long 种子设置此随机数生成器的种子。
import java.util.Random;
import java.lang.Math;
/**
* java 的随机数测试程序。共3种获取随机数的方法:
* (01)、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。
* (02)、通过Math.random()返回一个0到1之间的double值。
* (03)、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。
*
* @author skywang
* @email kuiwu-wang@163.com
*/
public class RandomTest{
public static void main(String args[]){
// 通过System的currentTimeMillis()返回随机数
testSystemTimeMillis();
// 通过Math的random()返回随机数
testMathRandom();
// 新建“种子为1000”的Random对象,并通过该种子去测试Random的API
testRandomAPIs(new Random(1000), " 1st Random(1000)");
testRandomAPIs(new Random(1000), " 2nd Random(1000)");
// 新建“默认种子”的Random对象,并通过该种子去测试Random的API
testRandomAPIs(new Random(), " 1st Random()");
testRandomAPIs(new Random(), " 2nd Random()");
}
/**
* 返回随机数-01:测试System的currentTimeMillis()
*/
private static void testSystemTimeMillis() {
// 通过
final long l = System.currentTimeMillis();
// 通过l获取一个[0, 100)之间的整数
final int i = (int)( l % 100 );
System.out.printf("\n---- System.currentTimeMillis() ----\n l=%s i=%s\n", l, i);
}
/**
* 返回随机数-02:测试Math的random()
*/
private static void testMathRandom() {
// 通过Math的random()函数返回一个double类型随机数,范围[0.0, 1.0)
final double d = Math.random();
// 通过d获取一个[0, 100)之间的整数
final int i = (int)(d*100);
System.out.printf("\n---- Math.random() ----\n d=%s i=%s\n", d, i);
}
/**
* 返回随机数-03:测试Random的API
*/
private static void testRandomAPIs(Random random, String title) {
final int BUFFER_LEN = 5;
// 获取随机的boolean值
boolean b = random.nextBoolean();
// 获取随机的数组buf[]
byte[] buf = new byte[BUFFER_LEN];
random.nextBytes(buf);
// 获取随机的Double值,范围[0.0, 1.0)
double d = random.nextDouble();
// 获取随机的float值,范围[0.0, 1.0)
float f = random.nextFloat();
// 获取随机的int值
int i1 = random.nextInt();
// 获取随机的[0,100)之间的int值
int i2 = random.nextInt(100);
// 获取随机的高斯分布的double值
double g = random.nextGaussian();
// 获取随机的long值
long l = random.nextLong();
System.out.printf("\n---- %s ----\nb=%s, d=%s, f=%s, i1=%s, i2=%s, g=%s, l=%s, buf=[",
title, b, d, f, i1, i2, g, l);
for (byte bt:buf)
System.out.printf("%s, ", bt);
System.out.println("]");
}
}
import java.util.*;
public class CreateRandom {
public float numRandom(){
float num;
Random r=new Random();
float value = (float) (Math.floor(r.nextFloat()*1000+0.5)/100);
Boolean b = r.nextBoolean();
if(b){
num = value;
}
else{
num=0-value;
}
return num;
}
public static void main(String[] args) {
CreateRandom cr = new CreateRandom();
float num = cr.numRandom();
System.out.print(num);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有