Java创建随机数的四种方式总结
作者:草木之花 时间:2022-05-11 10:29:17
第一次接触到随机数还是在c语言里面 使用的是 rand(); 但是重新执行一次的时候会发现,诶,居然和上一次执行的结果是一样的,因为没有初始化种子,所以系统用的都是统一的种子这时就会出现每次产生的随机数都一样,这时就会使用到 srand();这个随机数的发生器, 把种子给定当前的时间 即 srand((unsigned) time (&t)); 我c语言常用的随机数函数如下:
c语言随机数
#include <stdio.h>
int main(void) {
int left,right;
printf("请输入一个数:");
scanf_s("%d%d",&left,&right);
//srand((unsigned)time(NULL));
//for(int i = 0; i<10; i++)
//printf("%d\n",rand() % one);
//改进
for (int i = 0; i < 10; i++)
printf("随机值如下:%d\n", srandNext(left, right));
return 0;
}
/********************************************************************************
* @Function: srandNext
* @Description:Find a random integer from min to max
* @Author: caiziqi
* @Version: 1.1
* @formal parameter:min : is the minimum value of the value range of this random number function, max: is the maximum value range of this random number function
* @Date: 2022-7-26
* @Return : returns a random integer
********************************************************************************/
int srandNext(int max,int min) {
//To prevent users from passing parameters incorrectly
if (max <= min) {
if (max == min) {
return max;
}
int temp = max;
max = min;
min = temp;
}
unsigned int static seed = 0;
seed++;
srand((unsigned)time(NULL) + seed * seed);
return rand() % (max - min + 1) + min;
}
当然这不是最主要的今天是要讲我学到的java中的四种创建随机数的方法
java
以下代码都是以a ~ b 之间 求出一个随机整数
1.Random
创建对象
Random ra = new Random();
int right = (Math.max(a,b)) ;
int left = (Math.min(a,b);
int nu = (ra.nextInt(right) + left );
取值范围 left 最小值 right 最大值
2.SecureRandom
创建对象
SecureRandom sra = new SecureRandom();
int right = (Math.max(a,b)) ;
int left = (Math.min(a,b);
int nu = (sra .nextInt(right) + left );
取值范围 left 最小值 right 最大值
3.ThreadLocalRandom
创建对象
ThreadLocalRandom tra = ThreadLocalRandom.current();
求出随机数的左值和右值
int right = (Math.max(a,b)) ;
int left = (Math.min(a,b);
int nu = (tra .nextInt(right) + left );
取值范围 left 最小值 right 最大值
4.Math.Random
由于 Math 里面的方法是静态的所以可以直接 类名.方法名 使用
int number = (int)(a + Math.random()*(b-a+1)) //返回一个int类型的参数 所以用 int 接收
范围 a <= 随机数 < (b -a +1)
2 <= 随机数 < (4 -2 +1 )
完整代码
import java.security.SecureRandom;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class FourWaysOfRandomNumbers {
public static void main(String[] args) {
//用户输入两个数, 然后程序取这两个数里面的其中随机一个数
Scanner input = new Scanner(System.in);
System.out.print("请输入两个数:");
int a = input.nextInt();
int b = input.nextInt();
// 取值范围 left 最小值 right 最大值
int right = Math.max(a,b);
int left = Math.min(a,b);
int nu;
//四种生成随机数的方法
//第一种
Random ra = new Random();
while( true){
nu =(ra.nextInt(right) + left ) ;
System.out.println(nu);
//找到最大随机整数 输出并结束
if(nu == right) {
System.out.println(nu);
return;
}
}
//第二种
/*
SecureRandom sra = new SecureRandom();
while(true){
nu =(sra.nextInt(right) + left ) ;
System.out.println(nu);
//找到最大随机整数 输出并结束
if(nu == right) {
System.out.println(nu);
return;
}
}*/
//第三种 在多线程的时候使用 是最佳的; 因为它会为每个线程都 使用不同的种子
/* ThreadLocalRandom tra = ThreadLocalRandom.current();
while(true){
nu =(tra.nextInt(right) + left ) ;
System.out.println(nu);
//找到最大随机整数 输出并结束
if(nu == right) {
System.out.println(nu);
return;
}
}*/
/* //第四种
int value = (int)(a + Math.random()*(b-a+1));
System.out.println(value);
*/
}
}
来源:https://blog.csdn.net/m0_58520840/article/details/125991577
标签:Java,随机数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
OpenCV实现人脸识别简单程序
2023-07-07 00:31:12
JAVA基于SnakeYAML实现解析与序列化YAML
2023-11-26 06:14:44
分布式调度XXL-Job整合Springboot2.X实战操作过程(推荐)
2023-11-23 09:43:38
![](https://img.aspxhome.com/file/2023/9/59869_0s.jpg)
java安全编码指南之:Mutability可变性详解
2023-11-11 06:30:24
![](https://img.aspxhome.com/file/2023/3/59073_0s.jpg)
一文详解Java中流程控制语句
2023-11-26 11:39:49
java中Hashmap的get方法使用
2023-10-29 13:10:05
![](https://img.aspxhome.com/file/2023/2/58602_0s.png)
java GUI编程之paint绘制操作示例
2023-11-24 17:58:39
Java字节码ByteBuddy使用及原理解析上
2023-08-23 19:33:05
![](https://img.aspxhome.com/file/2023/0/58290_0s.png)
在Flutter中制作翻转卡片动画的完整实例代码
2023-06-23 23:31:21
IDEA部署JavaWeb项目到Tomcat服务器的方法
2023-11-02 23:21:22
SpringBoot异常处理器的使用与添加员工功能实现流程介绍
2021-10-21 19:24:23
![](https://img.aspxhome.com/file/2023/8/60908_0s.png)
Java超详细分析泛型与通配符
2023-07-28 08:34:26
![](https://img.aspxhome.com/file/2023/2/60592_0s.jpg)
通过实例解析Socket套接字通信原理
2023-11-02 20:17:35
![](https://img.aspxhome.com/file/2023/4/58684_0s.png)
Spring五大类注解读取存储Bean对象的方法
2023-11-09 22:07:07
![](https://img.aspxhome.com/file/2023/3/59003_0s.png)
Java数组与堆栈相关知识总结
2023-11-12 06:12:18
![](https://img.aspxhome.com/file/2023/7/59337_0s.jpg)
Android中外接键盘的检测的实现
2023-07-27 21:15:13
谈谈你可能并不了解的java枚举
2023-11-09 21:08:55
![](https://img.aspxhome.com/file/2023/1/59021_0s.png)
Java对xls文件进行读写操作示例代码
2023-08-04 17:55:31
SpringBoot详细讲解yaml配置文件的用法
2022-04-21 16:29:33
![](https://img.aspxhome.com/file/2023/0/61030_0s.png)
Android Studio和Gradle使用不同位置JDK的问题解决
2023-06-27 17:35:04
![](https://img.aspxhome.com/file/2023/2/84222_0s.png)