1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. import java.util.Random;
  56. /**
  57. * <p>Operations for random <code>String</code>s.</p>
  58. *
  59. * @author GenerationJava Core library
  60. * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
  61. * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
  62. * @author Stephen Colebourne
  63. * @author Gary Gregory
  64. * @author Phil Steitz
  65. * @since 1.0
  66. * @version $Id: RandomStringUtils.java,v 1.24 2003/08/22 17:25:33 ggregory Exp $
  67. */
  68. public class RandomStringUtils {
  69. /**
  70. * <p>Random object used by random method. This has to be not local
  71. * to the random method so as to not return the same value in the
  72. * same millisecond.</p>
  73. */
  74. private static final Random RANDOM = new Random();
  75. /**
  76. * <p><code>RandomStringUtils</code> instances should NOT be constructed in
  77. * standard programming. Instead, the class should be used as
  78. * <code>RandomStringUtils.random(5);</code>.</p>
  79. *
  80. * <p>This constructor is public to permit tools that require a JavaBean instance
  81. * to operate.</p>
  82. */
  83. public RandomStringUtils() {
  84. }
  85. // Random
  86. //-----------------------------------------------------------------------
  87. /**
  88. * <p>Creates a random string whose length is the number of characters
  89. * specified.</p>
  90. *
  91. * <p>Characters will be chosen from the set of all characters.</p>
  92. *
  93. * @param count the length of random string to create
  94. * @return the random string
  95. */
  96. public static String random(int count) {
  97. return random(count, false, false);
  98. }
  99. /**
  100. * <p>Creates a random string whose length is the number of characters
  101. * specified.</p>
  102. *
  103. * <p>Characters will be chosen from the set of characters whose
  104. * ASCII value is between <code>32</code> and <code>126</code> (inclusive).</p>
  105. *
  106. * @param count the length of random string to create
  107. * @return the random string
  108. */
  109. public static String randomAscii(int count) {
  110. return random(count, 32, 127, false, false);
  111. }
  112. /**
  113. * <p>Creates a random string whose length is the number of characters
  114. * specified.</p>
  115. *
  116. * <p>Characters will be chosen from the set of alphabetic
  117. * characters.</p>
  118. *
  119. * @param count the length of random string to create
  120. * @return the random string
  121. */
  122. public static String randomAlphabetic(int count) {
  123. return random(count, true, false);
  124. }
  125. /**
  126. * <p>Creates a random string whose length is the number of characters
  127. * specified.</p>
  128. *
  129. * <p>Characters will be chosen from the set of alpha-numeric
  130. * characters.</p>
  131. *
  132. * @param count the length of random string to create
  133. * @return the random string
  134. */
  135. public static String randomAlphanumeric(int count) {
  136. return random(count, true, true);
  137. }
  138. /**
  139. * <p>Creates a random string whose length is the number of characters
  140. * specified.</p>
  141. *
  142. * <p>Characters will be chosen from the set of numeric
  143. * characters.</p>
  144. *
  145. * @param count the length of random string to create
  146. * @return the random string
  147. */
  148. public static String randomNumeric(int count) {
  149. return random(count, false, true);
  150. }
  151. /**
  152. * <p>Creates a random string whose length is the number of characters
  153. * specified.</p>
  154. *
  155. * <p>Characters will be chosen from the set of alpha-numeric
  156. * characters as indicated by the arguments.</p>
  157. *
  158. * @param count the length of random string to create
  159. * @param letters if <code>true</code>, generated string will include
  160. * alphabetic characters
  161. * @param numbers if <code>true</code>, generatd string will include
  162. * numeric characters
  163. * @return the random string
  164. */
  165. public static String random(int count, boolean letters, boolean numbers) {
  166. return random(count, 0, 0, letters, numbers);
  167. }
  168. /**
  169. * <p>Creates a random string whose length is the number of characters
  170. * specified.</p>
  171. *
  172. * <p>Characters will be chosen from the set of alpha-numeric
  173. * characters as indicated by the arguments.</p>
  174. *
  175. * @param count the length of random string to create
  176. * @param start the position in set of chars to start at
  177. * @param end the position in set of chars to end before
  178. * @param letters if <code>true</code>, generated string will include
  179. * alphabetic characters
  180. * @param numbers if <code>true</code>, generated string will include
  181. * numeric characters
  182. * @return the random string
  183. */
  184. public static String random(int count, int start, int end, boolean letters, boolean numbers) {
  185. return random(count, start, end, letters, numbers, null, RANDOM);
  186. }
  187. /**
  188. * <p>Creates a random string based on a variety of options, using
  189. * default source of randomness.</p>
  190. *
  191. * <p>This method has exactly the same semantics as
  192. * {@link #random(int,int,int,boolean,boolean,char[],Random)}, but
  193. * instead of using an externally supplied source of randomness, it uses
  194. * the internal static {@link Random} instance.</p>
  195. *
  196. * @param count the length of random string to create
  197. * @param start the position in set of chars to start at
  198. * @param end the position in set of chars to end before
  199. * @param letters only allow letters?
  200. * @param numbers only allow numbers?
  201. * @param chars the set of chars to choose randoms from.
  202. * If <code>null</code>, then it will use the set of all chars.
  203. * @return the random string
  204. * @throws ArrayIndexOutOfBoundsException if there are not
  205. * <code>(end - start) + 1</code> characters in the set array.
  206. */
  207. public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) {
  208. return random(count, start, end, letters, numbers, chars, RANDOM);
  209. }
  210. /**
  211. * <p>Creates a random string based on a variety of options, using
  212. * supplied source of randomness.</p>
  213. *
  214. * <p>If start and end are both <code>0</code>, start and end are set
  215. * to <code>' '</code> and <code>'z'</code>, the ASCII printable
  216. * characters, will be used, unless letters and numbers are both
  217. * <code>false</code>, in which case, start and end are set to
  218. * <code>0</code> and <code>Integer.MAX_VALUE</code>.
  219. *
  220. * <p>If set is not <code>null</code>, characters between start and
  221. * end are chosen.</p>
  222. *
  223. * <p>This method accepts a user-supplied {@link Random}
  224. * instance to use as a source of randomness. By seeding a single
  225. * {@link Random} instance with a fixed seed and using it for each call,
  226. * the same random sequence of strings can be generated repeatedly
  227. * and predictably.</p>
  228. *
  229. * @param count the length of random string to create
  230. * @param start the position in set of chars to start at
  231. * @param end the position in set of chars to end before
  232. * @param letters only allow letters?
  233. * @param numbers only allow numbers?
  234. * @param chars the set of chars to choose randoms from.
  235. * If <code>null</code>, then it will use the set of all chars.
  236. * @param random a source of randomness.
  237. * @return the random string
  238. * @throws ArrayIndexOutOfBoundsException if there are not
  239. * <code>(end - start) + 1</code> characters in the set array.
  240. * @throws IllegalArgumentException if <code>count</code> < 0.
  241. * @since 2.0
  242. */
  243. public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars, Random random) {
  244. if (count == 0) {
  245. return "";
  246. } else if (count < 0) {
  247. throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
  248. }
  249. if ((start == 0) && (end == 0)) {
  250. end = 'z' + 1;
  251. start = ' ';
  252. if (!letters && !numbers) {
  253. start = 0;
  254. end = Integer.MAX_VALUE;
  255. }
  256. }
  257. StringBuffer buffer = new StringBuffer();
  258. int gap = end - start;
  259. while (count-- != 0) {
  260. char ch;
  261. if (chars == null) {
  262. ch = (char) (random.nextInt(gap) + start);
  263. } else {
  264. ch = chars[random.nextInt(gap) + start];
  265. }
  266. if ((letters && numbers && Character.isLetterOrDigit(ch))
  267. || (letters && Character.isLetter(ch))
  268. || (numbers && Character.isDigit(ch))
  269. || (!letters && !numbers)) {
  270. buffer.append(ch);
  271. } else {
  272. count++;
  273. }
  274. }
  275. return buffer.toString();
  276. }
  277. /**
  278. * <p>Creates a random string whose length is the number of characters
  279. * specified.</p>
  280. *
  281. * <p>Characters will be chosen from the set of characters
  282. * specified.</p>
  283. *
  284. * @param count the length of random string to create
  285. * @param chars the String containing the set of characters to use,
  286. * may be null
  287. * @return the random string
  288. * @throws IllegalArgumentException if <code>count</code> < 0.
  289. */
  290. public static String random(int count, String chars) {
  291. if (chars == null) {
  292. return random(count, 0, 0, false, false, null, RANDOM);
  293. }
  294. return random(count, chars.toCharArray());
  295. }
  296. /**
  297. * <p>Creates a random string whose length is the number of characters
  298. * specified.</p>
  299. *
  300. * <p>Characters will be chosen from the set of characters specified.</p>
  301. *
  302. * @param count the length of random string to create
  303. * @param chars the character array containing the set of characters to use,
  304. * may be null
  305. * @return the random string
  306. * @throws IllegalArgumentException if <code>count</code> < 0.
  307. */
  308. public static String random(int count, char[] chars) {
  309. if (chars == null) {
  310. return random(count, 0, 0, false, false, null, RANDOM);
  311. }
  312. return random(count, 0, chars.length, false, false, chars, RANDOM);
  313. }
  314. }