Java Random Fetch string tool class _java

Source: Internet
Author: User
Tags generator lowercase rand stringbuffer

How to generate Java random numbers

In Java, the concept of random numbers in general, there are three kinds.
1, through System.currenttimemillis () to obtain a current time of the number of milliseconds long number.
2. Returns a double value from 0 to 1 by Math.random ().
3, through the random class to produce a random number, this is a professional random tool class, powerful.

Second, Random class API description

1, Java API description
An instance of the random class is used to generate a pseudo random number stream. This class uses 48-bit seeds and modifies them using a linear congruence formula (see Donald Knuth, "The Art of Computer programming, Volume 2", sect. 3.2.1).

If two Random instances are created with the same seed, the same method call sequence is made for each instance, which generates and returns the same number sequence. In order to guarantee the implementation of the property, a specific algorithm is specified for the class Random.
Many applications will find the random method in the Math class easier to use.

2. Summary of methods
Random ()
Creates a new random number generator.

Random (Long Seed)
Create a new random number generator with a single long seed:
The public Random (long Seed) {setseed (seed);} Next method uses it to hold the state of the random number generator.

protected int Next (int bits)
Generates the next pseudo random number.

Boolean Nextboolean ()
Returns the next pseudo-random number, which is a uniformly distributed Boolean value taken from the sequence of the random number generator.

void Nextbytes (byte[] bytes)
Generates random bytes and places them in a user-supplied byte array.

Double nextdouble ()
Returns the next pseudo-random number, which is the evenly spaced double of 0.0 and 1.0 removed from the sequence of random number generators.

Float Nextfloat ()
Returns the next pseudo-random number, which is a float that is evenly distributed between 0.0 and 1.0 from a sequence of random number generators.

Double Nextgaussian ()
Returns the next pseudo-random number, which is a double value of Gaussian ("normal") distribution from the sequence of random number generators, with an average of 0.0 and a standard deviation of 1.0.

int Nextint ()
Returns the next pseudo-random number, which is the uniformly distributed int value in the sequence of this random number generator.

int nextint (int n)
Returns a pseudo-random number, which is an int value that is removed from the sequence of the random number generator, evenly distributed between 0 (including) and the specified value (excluding).

Long Nextlong ()
Returns the next pseudo-random number, which is a uniformly distributed long value from the sequence of the random number generator.

void Setseed (Long Seed)
Use a single long seed to set the seed for this random number generator.

Three, random class use explanation

1. The difference between seed and seeds
The random class uses an instance of a policy-striped seed and a random that does not carry a seed.
Popular said that the difference between the two is:
With seeds, each run produces the same result.
Without seeds, each run generated is random, there is no law to say.
2. Create random objects without seeds
Random Random = new Random ();
3. Create random objects without seeds
There are two ways of doing this:
1) Random Random = new Random (555L);
2) Random Random = new Random ();
Random.setseed (555L);

Iv. Comprehensive Application

Here's how to show the usage by using a recently written random number tool class:

Copy Code code as follows:

Import Java.util.Random;
/**
* Random number, then string tool
* User:leizhimin
* Date:2008-11-19 9:43:09
*/
public class Randomutils {
public static final String Allchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String Letterchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String Numberchar = "0123456789";
/**
* Returns a fixed-length random string (contains only uppercase and lowercase letters, numbers)
*
* @param length Random string lengths
* @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 lengths
* @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 lengths
* @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 lengths
* @return Random string
*/
public static String generateupperstring (int length) {
return generatemixstring (length). toUpperCase ();
}

/**
* Generate a fixed-length pure 0 string
*
* @param length string
* @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 from a number that is not sufficient for front 0
*
* @param num number
* @param fixdlenth string length
* @retur n 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 ("converts number + num +" to a string with a length of + Fixdlenth +) exception! ");
}
Sb.append (Strnum);
return sb.tostring ();
}

/**
* Generates a fixed-length string from a number that is not sufficient for front 0
*
* @param num number
* @param fixdlenth string length
* @retur n fixed-length string
/
public static string tofixdlengthstring (int num, int fixdlenth) {
StringBuffer sb = new S Tringbuffer ();
String strnum = string.valueof (num);
if (fixdlenth-strnum.length () >= 0) {
Sb.append (generatezerostring (Fixdlenth-strnum.length ()));
} else {
throw new RuntimeException ("converts number + num +" to a string with a length of + Fixdlenth +) exception! ");
}
Sb.append (Strnum);
return sb.tostring ();
}

public static void Main (string[] args) {
System.out.println (generatestring (15));
System.out.println (generatemixstring (15));
System.out.println (generatelowerstring (15));
System.out.println (generateupperstring (15));
System.out.println (generatezerostring (15));
System.out.println (tofixdlengthstring (123, 15));
System.out.println (Tofixdlengthstring (123L, 15));
}
}


Run Result:
Vwmbpinbzfgcphg
23hyraHdJkKPwMv
Tigowetbwkm1nde
bpz1knejphb115n
000000000000000
000000000000123
000000000000123
Process finished with exit code 0

Vi. Summary

1, random number is very commonly used in Java, there are three ways to produce, to random random number of the most complex use.
2, the random class object has whether to carry the seed the cent, with the seed as long as the seed is same, many times runs, produces the random number the result always is like that.
3. There are two kinds of seed objects with seed random numbers, the effect is the same. But the random number with seeds seems to be of little use.
4, random function covers the function of Math.random ().
5. Random numbers can be used to achieve complex random data such as random strings.
6, do not study the random number not repeat, the significance is not

Supplemental Code:

Copy Code code as follows:

Package com.test;
Import Java.util.Random;

public class Randomutils {
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 lengths
* @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 lengths
* @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 lengths
* @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 lengths
* @return Random string
*/
public static String generateupperstring (int length) {
return generatemixstring (length). toUpperCase ();
}

/**
* Generate a fixed-length pure 0 string
*
* @param length string
* @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 ();
}

/**
* To generate a fixed-length string based on the number, the length is not enough front 0
*
* @param num Numbers
* @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 ("Converts the number + num +" into a string with a length of "+ Fixdlenth +" an exception!) ");
}
Sb.append (Strnum);
return sb.tostring ();
}

/**
* To generate a fixed-length string based on the number, the length is not enough front 0
*
* @param num Numbers
* @param fixdlenth string length
* @return Fixed-length string
*/
public static String tofixdlengthstring (int 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 ("Converts the number + num +" into a string with a length of "+ Fixdlenth +" an exception!) ");
}
Sb.append (Strnum);
return sb.tostring ();
}

/**
* The number of Len digits per generation is different
* @param param
* @return Fixed-length figures
*/
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];
}
return result;
}

public static void Main (string[] args) {
System.out.println (generatestring (10));
System.out.println (generatemixstring (10));
System.out.println (generatelowerstring (10));
System.out.println (generateupperstring (10));
System.out.println (generatezerostring (10));
System.out.println (tofixdlengthstring (123, 10));
System.out.println (Tofixdlengthstring (123L, 10));
Int[] in = {1,2,3,4,5,6,7};
System.out.println (Getnotsimple (in,3));
}
}

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.