Java Random String createRandomString(final int size)

Here you can find the source of createRandomString(final int size)

Description

Creates a random string filled with hex values.

License

Apache License

Declaration

public static synchronized String createRandomString(final int size) 

Method Source Code

//package com.java2s;
/*/*  w w w.j a v a 2s.  c  o  m*/
 * Copyright 2009-2016 DigitalGlobe, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 *
 */

import java.util.*;

public class Main {
    private static Random random = new Random(System.currentTimeMillis());

    /**
     * Creates a random string filled with hex values.
     */
    public static synchronized String createRandomString(final int size) {
        // create a random string of hex characters. This will force the
        // sequence file to split appropriately. Certainly a hack, but shouldn't
        // cause much of a difference in speed, or storage.
        String randomString = "";
        while (randomString.length() < size) {
            randomString += Long.toHexString(random.nextLong());
        }
        return randomString.substring(0, size);
    }
}

Related

  1. createRandomString()
  2. createRandomString(int length)
  3. createRandomWord(boolean startWithCapital)
  4. generateRandom(int num)
  5. generateRandom(Integer digits)