Android Open Source - CommonClasses Random String






From Project

Back to project page CommonClasses.

License

The source code is released under:

GNU General Public License

If you think the Android project CommonClasses listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * This source is part of the CommonClasses repository.
 */*from  w w w  .j  a va 2  s  . co  m*/
 * Copyright 2014 Kevin Liu (airk908@gmail.com)
 *
 * CommonClasses is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CommonClasses is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CommonClasses.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.commonclasses.string;

import java.security.SecureRandom;
import java.util.Random;

/**
 * Random String Generator
 */
public class RandomString {
    private static int DEFAULT_STRING_LONG = 6;
    private static char[] CHARSET_AZ_09 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

    /**
     * Get a random string with default length
     *
     * @return the random string
     */
    public static String getRandomString() {
        return randomString(CHARSET_AZ_09, DEFAULT_STRING_LONG);
    }

    /**
     * Change the default length with new value
     *
     * @param newLength the new length you wanna change to
     * @return the default length
     */
    public static int changeDefaultLength(int newLength) {
        if (newLength > 0) {
            DEFAULT_STRING_LONG = newLength;
        }
        return DEFAULT_STRING_LONG;
    }

    private static String randomString(char[] characterSet, int length) {
        Random random = new SecureRandom();
        char[] result = new char[length];
        for (int i = 0; i < result.length; i++) {
            // picks a random index out of character set > random character
            int randomCharIndex = random.nextInt(characterSet.length);
            result[i] = characterSet[randomCharIndex];
        }
        return new String(result);
    }
}




Java Source Code List

com.github.commonclasses.logwrapper.LogWrapper.java
com.github.commonclasses.network.DownloadUtils.java
com.github.commonclasses.storage.FileOperations.java
com.github.commonclasses.string.RandomString.java
com.github.commonclasses.widget.ListViewForScrollView.java
com.github.commonclasses.widget.RoundedImageView.java