软件编程
位置:首页>> 软件编程>> java编程>> Java创建随机数的四种方式总结

Java创建随机数的四种方式总结

作者:草木之花  发布时间:2022-05-11 10:29:17 

标签:Java,随机数

第一次接触到随机数还是在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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com