public class Randomutil {
/**
* Generate an N-bit random number string
*
* @param n
* @return
*/
public static String getrandomnum (int length) {
String str = "0123456789";
Random random = new random ();
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < length; ++i) {
int number = Random.nextint (9);//[1,9]
Sb.append (Str.charat (number+1));
}
return sb.tostring ();
}
/**
* Generate a random n-bit string
*
* @param length
* @return
*/
public static String getrandomstring (int length) {
String str = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new random ();
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < length; ++i) {
int number = Random.nextint (36);//[0,36]
Sb.append (Str.charat (number));
}
return sb.tostring ();
}
/**
* Get current time, Month date
*
* @return
*/
public static String GetDate () {
String str = "";
DateFormat df = new SimpleDateFormat ("YYYYMMDDHH");
Date date = new Date ();
str = Df.format (date);
return str;
}
/**
* Generate user id,10 bit, pure digital
*
* @return
*/
public static String UserId () {
String userId = "";
UserId = Getrandomnum (10);
return userId;
}
/**
* Generate order number, 13-bit, pure digital
*
* @return
*/
public static String OrderId () {
String orderId = "";
String date = GetDate ();
String rand = getrandomnum (13);
OrderId = date + Rand;
return orderId;
}
/**
* Generate product id,10 bit, pure digital
*
* @return
*/
public static String proid () {
String userId = "";
UserId = Getrandomnum (10);
return userId;
}
/**
* Random Fetch list data
* @param list
* @param N to take a few
* @return
*/
@SuppressWarnings ({"Rawtypes", "unused", "unchecked"})
public static list getrandomlist (list list, int n) {
Map map = new HashMap ();
List listnew = new ArrayList ();
if (List.size () <=n) {
return list;
}else{
while (Map.size () <n) {
int random = (int) (Math.random () * list.size ());
if (!map.containskey (random)) {
Map.put (Random, "");
Listnew.add (List.get (random));
}
}
return listnew;
}
}
/**
* Take array random number
* @param arr
* @param n
* @return
*/
@SuppressWarnings ({"Unused", "unchecked"})
public static long[] Createrandomarray (long[] arr, int n) {
TODO auto-generated Method Stub
@SuppressWarnings ("Rawtypes")
Map map = new HashMap ();
long[] arrnew = new Long[n];
if (arr.length<=n) {
return arr;
}else{
int count = 0;//new array subscript count
while (Map.size () <n) {
int random = (int) (Math.random () * arr.length);
if (!map.containskey (random)) {
Map.put (Random, "");
arrnew[count++] = Arr[random];
}
}
return arrnew;
}
}
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