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;
        
    }
    

    
    
    
    
    

}

 

posted on 2021-07-27 20:40  sunny_2016  阅读(266)  评论(0编辑  收藏  举报

导航