zoukankan      html  css  js  c++  java
  • java生成随机数字

    import java.util.ArrayList;
    import java.util.Random;
    
    public class Demo_Random {
    
        public static void main(String[] args) {
    
            Random random = new Random();
            int a = random.nextInt(1000);
            System.out.println(a);
            
            Random ran1 = new Random();
            for(int i=0;i<10;i++) {
                System.out.print(ran1.nextInt(10) + " ");
            }
            System.out.println();
            Random ran2 = new Random();
            for(int i = 0;i<10;i++) {
                System.out.print(ran2.nextInt(10) + " ");
            }
            System.out.println();
            int i = (int)(Math.random()*100);
            System.out.println(i);
            
            
            String str = "1234567890";
            Random ran = new Random();
            StringBuffer sb = new StringBuffer();
            for(int j=0;j<str.length();j++) {
                
                int nu = ran.nextInt(str.length());
                char s = str.charAt(nu);
                sb.append(s);
            
                
            }
            System.out.println(sb.toString()); 
            
            char c = str.charAt(1);
            System.out.println(c);
            
            System.out.println("------------------");
            
            System.out.println(getDiffNo(10));
            
            
    
        }
        
        public static ArrayList getDiffNo(int n) {
            
            ArrayList list = new ArrayList();
            Random rand = new Random();
            boolean bool[] = new boolean[n];
            int num = 0;
            for(int i=0;i<n;i++) {
                do {
                    num = rand.nextInt(n);
                }while(bool[num]);
                bool[num] = true;
                list.add(num);
                    
            }
            
            return list;
            
        }
        
    
        
        
        
        
        
    
    }
  • 相关阅读:
    toj 2975 Encription
    poj 1797 Heavy Transportation
    toj 2971 Rotating Numbers
    zoj 2281 Way to Freedom
    toj 2483 Nasty Hacks
    toj 2972 MOVING DHAKA
    toj 2696 Collecting Beepers
    toj 2970 Hackle Number
    toj 2485 Card Tric
    js页面定位,相关几个属性
  • 原文地址:https://www.cnblogs.com/51testing/p/15067804.html
Copyright © 2011-2022 走看看