①:随机生成20个小于10的数
package IO;
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
Random random =new Random();
random.nextInt(10);
for(int i =0;i<20;i++){
System.out.print(random.nextInt(10));
}
}
}
②从5个人中每次随机生成两个人的名字
package IO;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class RandomTest1 {
public static void main(String[] args) {
Map map = new HashMap();
map.put(1, "张三");
map.put(2, "李四");
map.put(3, "王五");
map.put(4, "周六");
map.put(5, "孙七");
Random random = new Random();
Set set = new HashSet();
for (int i = 0; i < 3; i++) {
set.add(map.get((random.nextInt(5)) +1));
}
Iterator it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}