Here you can find the source of randomNumberString(int in)
Parameter | Description |
---|---|
in | - positive int |
public static String randomNumberString(int in)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static final Random gRandomizer = new Random(System.currentTimeMillis()); /**{ method// w w w .java 2s.c om @name randomNumberString @function - create a randomString of letters of length n @param in - positive int @return - the string length 1..256 }*/ public static String randomNumberString(int in) { in = Math.max(1, Math.min(in, 256)); StringBuffer sb = new StringBuffer(); for (int i = 0; i < in; i++) { char c = (char) Math.min('9', ('0' + randomInt(10))); sb.append(c); } return (sb.toString()); } /**{ method @name randomElement @function - choose an element of an array at random @param in - choices - non-null non-empty @return - one choice }*/ public static int randomInt(int in) { if (in <= 1) { if (in == 1) return (0); throw new IllegalArgumentException("randomInt must take a number > 1"); } int test = gRandomizer.nextInt(in); if (test < 0) test = -test; return (test); } /**{ method @name toString @function convert a char to a string @param c the char @return the String }*/ public static String toString(char c) { StringBuffer s = new StringBuffer(); s.append(c); return (s.toString()); } }