java.util.Random 类

时间:2024-01-07 17:24:02
//: object/ForEachFloat.java
package object; import java.util.Random;
public class ForEachFloat
{
public static void main(String[] args)
{
Random rand = new Random();//47是随机种子,如果不提供种子,默认为系统时间
float f[] = new float[10];
for(int i = 0; i < 10; i++)
f[i] = rand.nextFloat(); //生成0-1之间的float值
/*nextInt(100)+1 //100 + i是生成的int值的上限(不包括100+i),1为我们设定的下限(包括1),下限默认为0
* nextLong()
* nextDouble()
*/
for(int i = 0; i < 10; i++)
System.out.printf("%f\t",f[i]);
System.out.println('\n');
for(float x : f) //将f数组的值按顺序赋予x
System.out.printf("%f\t",x);
}
}