Java----Randomly generate content of a specified length

Source: Internet
Author: User

public class randomnumber{
Randomly generates a number + string or text of a specified length
public static String getrandomnumberandstring (int lengthofstring) {
int i=0;
int count=0;

Final String chars= "1, 2, 3, 4, 5, 6, 7, 8, 9, 0, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z";

Final String chars = "See, Ming, Q, Li, Li, er, point, text, several, fixed, this, public, special, do, outside, child, phase, West, fruit, walk, will, month, ten, real, to, sound, car, full, letter, heavy,"
+ "Three, machine, work, material, gas, every, and, don't, true, dozen, too, new, than, only, then, husband, again, book, Ministry, Water, like, eye, etc, body, but, add, electricity, the Lord, the boundary, the door, the benefit, the sea, the subject, listens, watches,"
+ "Germany, less, grams, generations, members, Xu, Apex, first, mouth, by, death, Ann, writing, sex, horse, light, white, or, living, difficult, hope, teaching, life, flowers, knot, music, color,, one, is, I, not, people, in, he,"
+ "There is, this, one, on, on, to, to, when, big, ground, for, son, in, you, say, birth, country, year, more, pull, East, God, remember, place, let, mother, father, should, straight, Word, field, customized, newspaper, friend, close, put,"
+ "To, Zhang, recognize, connect, Sue, enter, laugh, inside, British, military, Hou, people, age, toward, ho, degrees, mountains, sleep, road, belt, million, male, edge, wind, solution, call, Ren, Jin, fast, original, eat, Mom, change, pass, teacher, stand, elephant, number,"
+ "Four, Lost, full, war, far, lattice, shi, tone, light, eye, article, it, sickness, beginning, reach, deep, complete, present, mention, Seek, Qing, Wang, the, empty, industry, think, cut, how, not, find, film, Luo, Money, 紶,, language, Yuan, Xi,"
+ "Zeng, Depart, Fei, Ke, yan, gan, Liu, Huan, about, each, namely, refers to, is, the opposite, the question, must, the, the discussion, the intersection, the end, the forest, the medicine, the late, the system, the ball, the decision, the 窢, the biography, the painting, the guarantee, the reading, the transportation
+ "Quantity, bitter, fire, cloth, goods, near, sit, produce, answer, star, fine, visual, five, even, division, BA, QI, tube, category, not, friends, and, marriage, Taiwan, night, Green, north, team, long, depending on, the more, view, fall, do, shape, shadow, red, father, Hundred,"
+ "with, on, that, and, want, she, out, also, get, in, after, from, to, will, home, can, under, and, over, day, go, can, to, small, many, but, in, heart, study,,,", all, good, see, "
+ "Starting, sending, when, not, into, only, such as, things, put, still, use, first, kind, way, think, do, kind, open, beautiful, total, from, without, sentiment, self, face, the most, female, but, present, former, some, the, the same,"
+ "Day, hand, again, line, meaning, movement, square, period, it, head, Sutra, Long, son, back, bit, sub, love, old, reason, very, give, name, law, between,,, know, world, what, two, time, make, body, person,"
+ "Be, high, already, pro, its, into, this, words, often, with, live, positive, sense, A, B, C, Ding, E, self, g, Xin, Ren, Gui, son, ugly, Yin, Mao, Chen, have, noon, not, Shen, unitary, Xu, Hai";
String[] Chararr=chars.split (",");//with, as delimiter, in the array

StringBuffer randomstr = new StringBuffer ("");
Random rnd=new random ();
int strlen = Chararr.length;

while (Count < lengthofstring) {
i = Rnd.nextint (strlen);
Randomstr.append (Chararr[i]);
count++;
}
Return randomstr.tostring (). toLowerCase ();
}


Randomly generates numbers within a specified range, without the parameters themselves
public static int Getextentrandomnumber (int extent) {
int number = (int) (Math.random () * extent);
return number;
}

Main Method Test
public static void Main (string[] args) {
System.out.println (Getrandomnumberandstring (5));
System.out.println (Getextentrandomnumber (52));
}
}

Java----Randomly generate content of a specified length

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.