The most complete Java random number generation algorithm ever Shared

  • 2020-04-01 02:48:00
  • OfStack


String password = RandomUtil.generateString(10);

Source as follows:


package com.javaniu.core.util;
import java.util.Random;
public class RandomUtil {
 public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 public static final String NUMBERCHAR = "0123456789";
 
 public static String generateString(int length) {
  StringBuffer sb = new StringBuffer();
  Random random = new Random();
  for (int i = 0; i < length; i++) {
   sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
  }
  return sb.toString();
 }
 
 public static String generateMixString(int length) {
  StringBuffer sb = new StringBuffer();
  Random random = new Random();
  for (int i = 0; i < length; i++) {
   sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
  }
  return sb.toString();
 }
 
 public static String generateLowerString(int length) {
  return generateMixString(length).toLowerCase();
 }
 
 public static String generateUpperString(int length) {
  return generateMixString(length).toUpperCase();
 }
 
 public static String generateZeroString(int length) {
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < length; i++) {
   sb.append('0');
  }
  return sb.toString();
 }
 
 public static String toFixdLengthString(long num, int fixdlenth) {
  StringBuffer sb = new StringBuffer();
  String strNum = String.valueOf(num);
  if (fixdlenth - strNum.length() >= 0) {
   sb.append(generateZeroString(fixdlenth - strNum.length()));
  } else {
   throw new RuntimeException(" The digital " + num + " That translates to length is zero " + fixdlenth
     + " An exception occurred to the string of! ");
  }
  sb.append(strNum);
  return sb.toString();
 }
 
 public static int getNotSimple(int[] param, int len) {
  Random rand = new Random();
  for (int i = param.length; i > 1; i--) {
   int index = rand.nextInt(i);
   int tmp = param[index];
   param[index] = param[i - 1];
   param[i - 1] = tmp;
  }
  int result = 0;
  for (int i = 0; i < len; i++) {
   result = result * 10 + param[i];
  }
  return result;
 }
 public static void main(String[] args) {
  System.out.println(" Returns a random string of fixed length ( Contains only uppercase and lowercase letters and Numbers ):" + generateString(10));
  System.out
    .println(" Returns a random string of plain letters of a fixed length ( Contains only uppercase and lowercase letters ):" + generateMixString(10));
  System.out.println(" Returns a fixed-length random string of plain uppercase letters ( Contains only uppercase and lowercase letters ):"
    + generateLowerString(10));
  System.out.println(" Returns a random string of plain lowercase letters of a fixed length ( Contains only uppercase and lowercase letters ):"
    + generateUpperString(10));
  System.out.println(" It forms a constant length pure 0 string :" + generateZeroString(10));
  System.out.println(" Generates a fixed-length string based on the number 0:"
    + toFixdLengthString(123, 10));
  int[] in = { 1, 2, 3, 4, 5, 6, 7 };
  System.out.println(" Each generated len The Numbers are different :" + getNotSimple(in, 3));
 }
}

Picture is truth:

< img border = 0 SRC = "/ / img.jbzj.com/file_images/article/201401/2014118152344034.jpg" >

Java random character supplement

Today, I saw a demo of Java random character generation in zuidaimai. I just wanted to use it, but I found it incomplete. Please rearrange it and share it with friends in need


public static void main(String[] args) {    
        //String s = RandomNum.getRandomNumStr(5);
        //System.out.println(s);
        System.out.println(" generate 5 A contain 5 Character string: ");
        RandomNum.SuiJiZiFuChuan(5,5);
        System.out.println(" generate 3 A contain 6 Character string: ");
        RandomNum.SuiJiZiFuChuan(6,3);
        System.out.println(" Generate any 1 to 20 Contains any 1 to 10 Character string: ");
        RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),(int)(10*Math.random()));
        System.out.println(" Randomly generated characters: ");
        int i=0;
        while(i<(int)(10*Math.random())){
            RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),1);
            i++;
        }
    }    
    public static void SuiJiZiFuChuan(int x,int y){
        for(int j=0;j<y;j++){
            for(int i=0;i<x;i++){
                int a=(int)(100*Math.random()+100*Math.random());
                while(true){
                    if(a>96&a<123)
                        break;
                    else
                        a=(int)(100*Math.random()+100*Math.random());
                }
                System.out.print((char)a);
            }
            System.out.println();
        }
    }

Execution results:

< img border = 0 SRC = "/ / img.jbzj.com/file_images/article/201401/20140119001651.gif" >

Source: http://www.zuidaima.com/share/1585762703215616.htm


Related articles: