一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java随机数的几种方法

时间:2011-06-24 编辑:简简单单 来源:一聚教程网

java随机数的几种方法

 

方法1:

 

 1 //利用固定数组长度n,通过n的变化来实现。
 2  //关键:nums[r]=nums[n-1];
 3  //        n--;
 4 public class Test1
 5 {
 6     public static void main(String[] args){
 7         int n=35;
 8         int[] nums=new int[n];
 9         for(int i=0;i 10             nums[i]=i+1;
11         int[] arr=new int[7];
12         for(int i=0;i 13             int r=(int)(Math.random()*n);
14             arr[i]=nums[r];
15             nums[r]=nums[n-1];
16             n--;
17         }
18         for(int i=0;i 19             System.out.print(arr[i]+"  ");
20         }
21     }
22 }

 

 


方法2:

 1 //利用循环实现
 2 public class Test2 
 3 { 
 4     public static void main(String[] args){ 
 5         int[] arr = new int[7]; 
 6         for (int i = 0; i < 7; i++){ 
 7             arr[i] = (int) (Math.random() * 35) + 1; 
 8             for (int j=0; j  9                 if (arr[j] == arr[i]){//如果arr[i]与arr[j]相同,则arr[i]重新取值,并检验
10                     i--;
11                     break;
12                 }
13             }
14         }
15         for(int i=0;i<7; i++) 
16         System.out.print(arr[i] + " "); 
17     }
18 }

 

 


方法3:

 

 1 //一个固定的无重复的数组,然后把这个数组随机调换位置
 2 //多次之后这个数组就是一个无重复的随机数组了
 3 public class Test3
 4 {
 5     public static void main(String[] args){
 6         int n=35;
 7         int[] nums=new int[n];
 8         for(int i=0;i  9             nums[i]=i+1;
10         int temp1,temp2,temp3;
11         for(int i=0;i 12             temp1=(int)(Math.random()*n);//随机产生一个位置
13             temp2=(int)(Math.random()*n);//随机产生另一个位置
14             if(temp1!=temp2){
15                 temp3=nums[temp1];
16                 nums[temp1]=nums[temp2];
17                 nums[temp2]=temp3;
18             }
19         }
20         int[] arr=new int[7];
21         for(int i=0;i 22             arr[i]=nums[i];
23             System.out.print(arr[i]+"  ");
24         }
25     }
26 }

 

 


方法4:

 

 1 //使用HashSet来实现
 2 import java.util.*;
 3 public class Test4
 4 {
 5     public static void main(String[] arg){
 6         int n=35;
 7         Set mySet=new HashSet();
 8         while(mySet.size()<7)
 9             mySet.add((int)(Math.random()*n)+1);
10         for(Integer i:mySet)
11             System.out.print(i+"  ");
12     }
13 }

 

 


方法5:

 

 1 import java.util.*;
 2 //使用linkedList来实现
 3 public class Test5
 4 {
 5     public static void main(String[] args){
 6         LinkedList mylist=new LinkedList();
 7         int n=35;
 8         for(int i=0;i  9             mylist.add(i+1);
10         int[] arr=new int[7];
11         for(int i=0;i 12             arr[i]=mylist.remove((int)(Math.random()*n));//remove(index i)移除指定位置处得元素
13             n--;
14         }
15         for(int i=0;i 16             System.out.print("arr["+i+"]:"+arr[i]+"  ");
17         }
18     }
19 }

 

 


最后再放一个小案例吧!彩票35选7,7位数均不同。仅供娱乐……

 

View Code  1 import javax.swing.*;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 public class Lottery extends JFrame implements ActionListener {
 5
 6     private int[] nums;//用来存放已抽取的随机数
 7     JTextField jtf=null;
 8     JButton jb=null;
 9    
10     public Lottery(){
11         super("猜数游戏");
12         nums=new int[7];
13         jtf=new JTextField(30);
14         jtf.setText("点击按钮,抽取7个1-35之间的数");
15         jb=new JButton("抽取");
16         setLayout(new FlowLayout());
17         jb.addActionListener(this);
18         add(jtf);
19         add(jb);
20         setSize(500,80);
21         setVisible(true);
22         setResizable(false);
23         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24     }
25    
26     public void actionPerformed(ActionEvent e) {
27         if(e.getSource()==jb){
28             getNums();
29             jtf.setText("7个数字为:"+nums[0]+", "+nums[1]+", "+nums[2]+", "+nums[3]+", "+nums[4]+", "+nums[5]+", "+nums[6]);
30         }
31     }
32     //抽取1——35之间的随机数
33     private int getRandom(){
34         return (int)(Math.random()*35)+1;
35     }
36     //获取随机数数组
37     private void getNums(){
38         for(int i=0;i 39             nums[i]=getRandom();
40             for(int j=0;j 41                 if(nums[j]==nums[i]){
42                     i--;
43                     break;
44                 }
45             }
46         }
47     }
48     public static void main(String[] args) {
49         Lottery testLot=new Lottery();
50     }
51
52 }

热门栏目