Java random number methods

Source: Internet
Author: User

Java random number methods

 

Method 1:

 

1 // use the fixed array length n, which is achieved through the change of n.
2 // key: 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 <nums. length; I ++)
10 nums [I] = I + 1;
11 int [] arr = new int [7];
12 for (int I = 0; I <arr. length; 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 <arr. length; I ++ ){
19 System. out. print (arr [I] + "");
20}
21}
22}

 

 


Method 2:

1 // implement through loops
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 <I; j ++ ){
9 if (arr [j] = arr [I]) {// if arr [I] is the same as arr [j], the arr [I] value is re-set and verified.
10 I --;
11 break;
12}
13}
14}
15 for (int I = 0; I <7; I ++)
16 System. out. print (arr [I] + "");
17}
18}

 

 


Method 3:

 

1 // a fixed non-repeated array, and then randomly positions the array
2 // after multiple times, this array is a random array with no duplicates
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 <nums. length; I ++)
9 nums [I] = I + 1;
10 int temp1, temp2, temp3;
11 for (int I = 0; I <nums. length; I ++ ){
12 temp1 = (int) (Math. random () * n); // randomly generate a location
13 temp2 = (int) (Math. random () * n); // randomly generate another location
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 <arr. length; I ++ ){
22 arr [I] = nums [I];
23 System. out. print (arr [I] + "");
24}
25}
26}

 

 


Method 4:

 

1 // use HashSet to implement
2 import java. util .*;
3 public class Test4
4 {
5 public static void main (String [] arg ){
6 int n = 35;
7 Set <Integer> mySet = new HashSet <Integer> ();
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}

 

 


Method 5:

 

1 import java. util .*;
2 // use consumer list
3 public class Test5
4 {
5 public static void main (String [] args ){
6. Sort List <Integer> mylist = new sort List <Integer> ();
7 int n = 35;
8 for (int I = 0; I <n; I ++)
9 mylist. add (I + 1 );
10 int [] arr = new int [7];
11 for (int I = 0; I <arr. length; I ++ ){
12 arr [I] = mylist. remove (int) (Math. random () * n); // remove (index I) remove the element obtained at the specified position
13 n --;
14}
15 for (int I = 0; I <arr. length; I ++ ){
16 System. out. print ("arr [" + I + "]:" + arr [I] + "");
17}
18}
19}

 

 


Let's end with another small case! The numbers of 7 or 35 lottery tickets vary. For entertainment only ......

 

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; // used to store the extracted random number
7 JTextField jtf = null;
8 JButton jb = null;
 9    
10 public Lottery (){
11 super ("guessing game ");
12 nums = new int [7];
13 jtf = new JTextField (30 );
14 jtf. setText ("Click to extract seven numbers between 1 and 35 ");
15 jb = new JButton ("extract ");
16 setLayout (new FlowLayout ());
17 jb. addActionListener (this );
18 add (jtf );
19 add (jb );
20 setSize (, 80 );
21 setVisible (true );
22 setResizable (false );
23 setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
24}
25
26 public void actionreceivmed (ActionEvent e ){
27 if (e. getSource () = jb ){
28 getNums ();
29 jtf. setText ("7 digits:" + nums [0] + "," + nums [1] + "," + nums [2] + ", "+ nums [3] +", "+ nums [4] +", "+ nums [5] +", "+ nums [6]);
30}
31}
32 // extract a random number between 1-35
33 private int getRandom (){
34 return (int) (Math. random () * 35) + 1;
35}
36 // Obtain the random number array
37 private void getNums (){
38 for (int I = 0; I <nums. length; I ++ ){
39 nums [I] = getRandom ();
40 for (int j = 0; j <I; 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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.