import java.util.Random;
import java.util.Scanner;
public class rand {
public static void main(String[] args) {
System.out.println("情输入一个数字:");//提示输入数字
Scanner sc = new Scanner(System.in);//获得用户输入
int i = sc.nextInt();//获取的是整数
for (int j = 0; j < i; j++) {//根据输入的整数确定循环次数 jSystem.out.println(new Random().nextInt());//new Random() 是生成一个随即数的对象,nextInt()是生成随机数的方法
}
}
}
存成rand.java 就可以先javac编译 再用java运行了
public class rand {
private Random random = new Random();
private int n = 0;
public void printRandom (){
Scanner input=new Scanner(System.in);
System.out.println("输入一个正整数");
this.n = input.nextInt();
for( int i=0;i
}
}
public static void main(String args[]){
rand t = new rand();
t.printRandom();
}
}
生成从10到20的随机数:
// rand.java
import java.util.Scanner;
public class rand {
private static final int MIN = 10;
private static final int MAX = 20;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入个数: ");
int n = input.nextInt();
for (int i = 0; i < n; i++) {
System.out.println((int) ((MAX - MIN + 1) * Math.random() + MIN));
}
}
}
试一下看行不,我徒手写的。
import java.util.*;
import java.util.Random;
public class RandomTest {
public static void main (String args[]) {
int num = System.in.read();
Random random =new java.util.Random(25);
for (int i = 0; i
}
}
}
随机数的范围说一下