zoukankan      html  css  js  c++  java
  • 生成不重复的随机数

    package com.demo.sw.test;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    /**
     * 
     * @author Mr.J
     *
     */
    public class NoRepeatNums {
    	
    	public static void main(String[] args) {
    		
    		int [] arr = getNoRepeatNums(3,1,102);
    		
    		
    		for (int i = 0; i < arr.length; i++) {
    			System.out.print(arr[i]+" ");
    		}
    		
    	}
    	
    	
    	/**
    	 * 从 from 到  to 取  n 个部重复的数
    	 * @param n
    	 * @param from
    	 * @param to
    	 * @return
    	 */
    	public static int[] getNoRepeatNums(int n, int from, int to){
    		int fl = (from+"").length();
    		int tl = (to+"").length();
    		//int fmin = tl<fl?tl:fl;
    		int tmax = tl>fl?tl:fl;
    		int f = from<to?from:to;
    		int t = from>to?from:to;
    		if((t-f)<n){
    			int []rtArr = new int [t-f+1] ;
    			int it = 0;
    			for (int i = f; i <= t; i++) {
    				rtArr[it]= i;
    				it++;
    			}
    			return rtArr;
    		}
    
    		//int min = Integer.parseInt(appendChar("1",'0',fmin));
    		int max = Integer.parseInt(appendChar("1",'0',tmax));
    		int num = -1;
    		Set s = new HashSet();
    		while(true){
    			if(null !=s && s.size() >= n){
    				break;
    			}
    			
    			num = (int)(Math.random()*max);
    			if(f<=num&&num<=t){
    				if(!s.contains(num)){
    					s.add(num);
    				}
    			}
    			
    		}
    		
    		int [] arr = new int[n];
    		
    		Iterator itr = s.iterator();
    		int i = 0;
    		while(itr.hasNext()){
    			Integer in = (Integer)itr.next();
    			arr[i] = in.intValue();
    			i++;
    		}
    		
    		return arr;
    		
    	}
    	
    	/**
    	 * 在字符串 S 后追加  num 个 C 并返回
    	 * @param s
    	 * @param c
    	 * @param num
    	 * @return
    	 */
    	public static String appendChar(String s, char c, int num){
    		for (int i = 0; i < num; i++) {
    			s+=c;
    		}
    		return s;
    	}
    	
    	
    	/**
    	 * 默认方法从 10 中取出 n 个部重复的数,n 必须小于或等于10 否则无法返回null
    	 * @param n
    	 * @return
    	 */
    	public static int[] getDefaultNoRepeatNums (int n) {
    		if(n>10){
    			return null;
    		}
    		
    		Set s = new HashSet();
    		while(true){
    			if(null !=s && s.size() >= n){
    				break;
    			}
    			
    			int num = (int)(Math.random()*10);
    			if(!s.contains(num)){
    				s.add(num);
    			}
    		}
    		
    		int [] arr = new int[n];
    		
    		Iterator itr = s.iterator();
    		int i = 0;
    		while(itr.hasNext()){
    			Integer in = (Integer)itr.next();
    			arr[i] = in.intValue();
    			i++;
    		}
    		
    		return arr;
    	}
    
    
    }
  • 相关阅读:
    NodeJS Stream 五:双工流
    NodeJS Stream 四:Writable
    NodeJS Stream 三:readable
    NodeJS Stream 二:什么是 Stream
    NodeJS stream 一:Buffer
    JavaScript 精粹
    详解k8s中的liveness和readiness的原理和区别_转
    通过 GoLang 全面了解 HTTPS_转自知乎-腾讯技术
    golang之vim和vscode开发环境
    go命令工具补遗
  • 原文地址:https://www.cnblogs.com/icenter/p/1988500.html
Copyright © 2011-2022 走看看