The most complete Java random number generation algorithm in history sharing _java

Source: Internet
Author: User

Copy Code code as follows:

String Password = randomutil.generatestring (10);

The source code is as follows:

Copy Code code 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";

 /**
  * Returns a fixed-length random string (contains only uppercase and lowercase letters, numbers)
  *
  * @param length
  *             random string length
  * @return Random string
  */
  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 ();
 }

/**
* Returns a fixed-length random plain letter string (containing only uppercase and lowercase letters)
*
* @param length
* Random string length
* @return Random string
*/
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 ();
}

/**
* Returns a fixed-length random plain capital string (containing only uppercase and lowercase letters)
*
* @param length
* Random string length
* @return Random string
*/
public static String generatelowerstring (int length) {
return generatemixstring (length). toLowerCase ();
}

/**
* Returns a fixed-length random plain lowercase letter string (contains only uppercase and lowercase letters)
*
* @param length
* Random string length
* @return Random string
*/
public static String generateupperstring (int length) {
return generatemixstring (length). toUpperCase ();
}

/**
* Generate a fixed-length pure 0 string
*
* @param length
* String length
* @return Pure 0 string
*/
public static String generatezerostring (int length) {
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < length; i++) {
Sb.append (' 0 ');
}
return sb.tostring ();
}

 /**
  * Generates a fixed-length string based on numbers, not enough for front 0
  *
  * @param num
  *             Digital
  * @param fixdlenth
  *             string Length
  * @return Fixed length string
  */
 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 (convert number + num + to length to + fixdlenth
 & Nbsp;   + "string has an exception! ");
  }
  sb.append (Strnum);
  return sb.tostring ();
 }

 /**
  * The number of Len digits generated each time is different
  *
  * @param param
  * @return Fixed-length number
  */
& nbsp;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 * + param[i];
&NBSP;&NBSP}
  return result;
 .}

public static void Main (string[] args) {
System.out.println ("Returns a fixed-length random string (containing only uppercase and lowercase letters, numbers):" + generatestring (10));
System.out
. println ("Returns a fixed-length random plain letter string (containing only uppercase and lowercase letters):" + generatemixstring (10));
System.out.println ("Returns a fixed-length random plain capital string (containing only uppercase and lowercase letters):"
+ generatelowerstring (10));
System.out.println ("Returns a fixed-length random pure lowercase letter string (containing only uppercase and lowercase letters):"
+ generateupperstring (10));
System.out.println ("Generate a fixed-length pure 0 string:" + generatezerostring (10));
SYSTEM.OUT.PRINTLN ("Generates a fixed-length string from a number that is not long enough to fill 0 in front:"
+ tofixdlengthstring (123, 10));
Int[] in = {1, 2, 3, 4, 5, 6, 7};
System.out.println ("The number of Len digits per generation is different:" + getnotsimple (in, 3));
}
}


There is a picture of the truth:

Java Random character Supplemental Edition

Today in Zuidaimai to see a Java random character generation demo, just need to use, but found incomplete, to reorganize, to share to the needy friends

Copy Code code as follows:

public static void Main (string[] args) {
String s = randomnum.getrandomnumstr (5);
System.out.println (s);
System.out.println ("Generate 5 strings containing 5 characters:");
Randomnum.suijizifuchuan (5,5);
System.out.println ("Generate 3 strings containing 6 characters:");
Randomnum.suijizifuchuan (6,3);
SYSTEM.OUT.PRINTLN ("Generate any 1 to 20 strings containing any 1 to 10 characters:");
Randomnum.suijizifuchuan ((int) (20*math.random ()), (int) (10*math.random ()));
SYSTEM.OUT.PRINTLN ("Random generation Character:");
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:



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

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.