随机数与枚举

来源:互联网 发布:如何在知乎添加话题 编辑:程序博客网 时间:2024/05/21 06:57

构建随机数生成器对象:Random rand = new Random();

构建方法使用了long型的随机数种子:Random rand2 = new Random(12345L);

生成0~100(不含100,即0~99)的int型随机数:int ir = rand.nextInt(100);


1.抽取10个不考虑重复情况的随机数:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package 随机数;  
  2. import java.util.*;  
  3.   
  4. public class Randomx {  
  5.   
  6.     public static void main(String[] args) {  
  7.         Random rand = new Random();  
  8.          int nums[] = new int[10];  
  9.          for(int i=0;i<10;i++)  
  10.          {  
  11.              nums[i]=rand.nextInt(100)+1;  
  12.          }  
  13.          Arrays.sort(nums);  
  14.          System.out.println("1~100范围内的10个随机数如下:");  
  15.          System.out.println(Arrays.toString(nums));  
  16.   
  17.     }  
  18.   
  19. }  
  20. /* 
  21. 输出如下: 
  22. 1~100范围内的10个随机数如下: 
  23. [3, 20, 26, 32, 40, 50, 61, 90, 97, 97] 
  24. */  


2.因此要生成10个相互不重复的随机数,必须在每次生成随机数时,把它一次与保存在数组中的各个随机数比较,如果重复,则再调用一次nextInt方法重新生成一次。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package 随机数;  
  2. import java.util.*;  
  3.   
  4. public class Randomx {  
  5.   
  6.     public static void main(String[] args) {  
  7.         Random rand = new Random();  
  8.          int nums[] = new int[10];  
  9.          for(int i=0;i<10;i++)  
  10.          {  
  11.              //循环标记  
  12.              whiletag:while(true)  
  13.              {  
  14.                  int randNum=rand.nextInt(100)+1;  
  15.                  for(int j=0;j<i;j++)  
  16.                  {  
  17.                      //相等则继续循环  
  18.                      if(randNum==nums[j])  
  19.                      {  
  20.                          continue whiletag;  
  21.                      }  
  22.                  }  
  23.                  //直到不重复时,将随机数保存到数组  
  24.                  nums[i]=randNum;  
  25.                  break whiletag;  
  26.              }  
  27.          }  
  28.          Arrays.sort(nums);  
  29.          System.out.println("1~100范围内的10个随机数如下:");  
  30.          System.out.println(Arrays.toString(nums));  
  31.   
  32.     }  
  33.   
  34. }  
  35. /* 
  36. 输出如下: 
  37. 1~100范围内的10个随机数如下: 
  38. [5, 15, 20, 55, 72, 83, 87, 90, 95, 100] 
  39. */  

3.定义抽奖方法,含有排除号码(例如抽了1等奖防止重复)
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package 抽奖_排除;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.Random;  
  5.   
  6. class Raffle2 {  
  7.     //抽号方法,有4个参数:起始号,终止号,抽奖个数,排除号码(可变参数)  
  8.     public static int[] raffleNums(int from,int to,int amount,int...exclusion)throws Exception{  
  9.         if((to-from+1)-exclusion.length<amount){  
  10.             throw new Exception("抽取范围内的数量必须不小于要抽取的个数");  
  11.         }  
  12.         Random rand=new Random();  
  13.         int nums[]=new int[amount];  
  14.         for(int i=0;i<amount;i++){  
  15.             whiletag:while(true)  
  16.             {  
  17.                 int randNum=rand.nextInt(to-from+1)+from;  
  18.                   
  19.                 for(int j=0;j<i;j++)  
  20.                 {  
  21.                     if(randNum==nums[j])  
  22.                     {  
  23.                             continue whiletag;  
  24.                     }  
  25.                 }  
  26.                 for(int k=0;k<exclusion.length;k++)  
  27.                 {  
  28.                     if(randNum==exclusion[k])  
  29.                     {  
  30.                         continue whiletag;  
  31.                     }  
  32.                 }  
  33.                 nums[i]=randNum;  
  34.                 break whiletag;  
  35.             }  
  36.         }  
  37.         return nums;  
  38.     }  
  39.   
  40. }  
  41. public class Raffle  
  42. {  
  43.     public static void main(String[] args) {  
  44.         try{  
  45.             System.out.println("抽取20~40范围内6个数,排除25和35,结果如下:");  
  46.             int nums[]=Raffle2.raffleNums(2040635,25);  
  47.             Arrays.sort(nums);  
  48.             System.out.println(Arrays.toString(nums));  
  49.             System.out.println("抽取1~10范围内5个数,排除2,3,4,结果如下:");  
  50.             int nums2[]=Raffle2.raffleNums(11052,3,4);  
  51.             Arrays.sort(nums2);  
  52.             System.out.println(Arrays.toString(nums2));  
  53.             System.out.println("抽取101~200范围内8个数,结果如下:");  
  54.             int nums3[]=Raffle2.raffleNums(1012008);  
  55.             Arrays.sort(nums3);  
  56.             System.out.println(Arrays.toString(nums3));  
  57.         }  
  58.         catch(Exception e){  
  59.             System.out.println("异常:"+e.getMessage());  
  60.         }  
  61.   
  62.     }  
  63. }  
  64. /* 
  65. 输出结果如下: 
  66. 抽取20~40范围内6个数,排除25和35,结果如下: 
  67. [20, 21, 23, 24, 30, 31] 
  68. 抽取1~10范围内5个数,排除2,3,4,结果如下: 
  69. [5, 6, 8, 9, 10] 
  70. 抽取101~200范围内8个数,结果如下: 
  71. [104, 124, 137, 143, 147, 151, 161, 180] 
  72. */  
0 0
原创粉丝点击