浅析Java随机数与定时器
作者:彬菌 时间:2022-06-04 16:21:10
产生90-100的重复的随机数:
public class RandomTest {
public static void main(String[] args){
/*
* Math.random()方法默认double类型,所以需要强制转换为int
*/
int x=(int)(Math.random()*(100-90+1)+90); //(max-min+1)+min=min-max
System.out.println(x);
}
}
产生90-100不重复的随机数:
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class RandomTest {
public static void main(String args[]){
int max=100; //最大值
int min=90; //最小值
int count=max-min; //随机数个数
Random random = new Random();
Set<Integer> set=new HashSet<>(); //hashset容器中只能存储不重复的对象
while(set.size()<count){ //hashset储存的元素数目
int x = random.nextInt(max-min+1)+min; //产生随机数
set.add(x); //把随机数添加到hashset容器中
}
for(int i:set){ //foreach遍历容器元素
System.out.println(i);
}
}
}
每一秒产生90-100的重复的随机数:
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class RandomTest {
void timer(){
Timer timer = new Timer(); //创建定时对象
timer.schedule(new TimerTask() {
public void run() { //TimerTask实现 Runnable接口的run方法
Random random = new Random();
int x = random.nextInt(100-90+1)+90; //(max-min+1)+min=min至max
// int x=random.nextInt(100)%(100-90+1) + 90; //同样的效果
System.out.println(x);
}
},0,1000); //0表示无延迟,1000ms=1s
}
public static void main(String[] args){
RandomTest ran=new RandomTest();
ran.timer(); //调用定时任务
}
}
本文转载于:https://www.idaobin.com/archives/301.html
标签:Java,随机数,定时器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈java面向对象中四种权限
2023-03-09 18:43:46
windows下java环境变量的设置方法
2022-12-01 03:13:14
java中方法递归的简单示例
2022-11-25 11:45:12
![](https://img.aspxhome.com/file/2023/1/66041_0s.png)
关于Mybatis的@param注解及多个传参
2021-09-10 21:17:21
深入理解Java设计模式之抽象工厂模式
2023-11-28 12:11:34
![](https://img.aspxhome.com/file/2023/6/60336_0s.jpg)
SpringBoot实现单文件与多文件上传功能
2023-03-22 23:44:11
![](https://img.aspxhome.com/file/2023/9/76229_0s.png)
Java多线程 Producer and Consumer设计模式
2022-08-30 07:13:24
Java毕业设计实战之医院心理咨询问诊系统的实现
2022-07-04 19:02:21
![](https://img.aspxhome.com/file/2023/5/74995_0s.jpg)
Maven中怎么手动添加jar包到本地仓库详解(repository)
2023-09-08 07:33:20
![](https://img.aspxhome.com/file/2023/3/82013_0s.jpg)
Java IO流之字符流的使用详解
2023-10-18 11:28:08
c#在excel中添加超链接示例分享
2023-06-03 14:10:45
android使用PullToRefresh实现下拉刷新和上拉加载
2023-08-06 11:06:58
![](https://img.aspxhome.com/file/2023/5/85455_0s.png)
Java继承Thread类创建线程类示例
2023-08-04 20:27:08
SpringBoot动态修改日志级别的操作
2022-10-31 05:00:30
![](https://img.aspxhome.com/file/2023/6/67116_0s.png)
Java深入了解数据结构中常见的排序算法
2021-11-02 23:33:53
![](https://img.aspxhome.com/file/2023/8/68138_0s.png)
Java聊天室之解决连接超时问题
2021-06-07 16:30:54
![](https://img.aspxhome.com/file/2023/5/69115_0s.png)
Springcloud Nacos基本操作代码实例
2023-07-07 18:24:22
![](https://img.aspxhome.com/file/2023/8/61148_0s.png)
自定义类加载器以及打破双亲委派模型解析
2023-06-22 22:03:59
![](https://img.aspxhome.com/file/2023/0/57400_0s.png)
如何只返回实体类中的部分字段问题
2022-11-28 05:09:53
![](https://img.aspxhome.com/file/2023/2/61282_0s.jpg)
Spring整合mybatis实现过程详解
2022-05-23 02:10:23
![](https://img.aspxhome.com/file/2023/5/71605_0s.png)