首頁  >  文章  >  Java  >  Java創建隨機數的方式有哪些

Java創建隨機數的方式有哪些

WBOY
WBOY轉載
2023-05-01 14:52:151180瀏覽

    第一次接觸到隨機數字還是在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);
    */
    
    
        }
    
    }

    以上是Java創建隨機數的方式有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除