Java Random generateRandom(byte[] byteArray)

Here you can find the source of generateRandom(byte[] byteArray)

Description

Generates a number of random alpha-numeric characters in US-ASCII

License

Open Source License

Parameter

Parameter Description
byteArray the byte array that will contain the newly generated bytes. the number of generated bytes is given by the length of the byteArray

Declaration

public static void generateRandom(byte[] byteArray) 

Method Source Code


//package com.java2s;
/* Copyright ? Jo?o Antunes 2008
 This file is part of MSRP Java Stack./*from   w  w  w .  j a  v  a 2 s. co  m*/
    
MSRP Java Stack is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
MSRP Java Stack 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 Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with MSRP Java Stack.  If not, see <http://www.gnu.org/licenses/>.
    
 */

import java.util.*;

public class Main {
    public static Random randomGenerator = new Random();

    /**
     * Generates a number of random alpha-numeric characters in US-ASCII
     * 
     * @param byteArray the byte array that will contain the newly generated
     *            bytes. the number of generated bytes is given by the length of
     *            the byteArray
     * 
     **/
    public static void generateRandom(byte[] byteArray) {
        int i;
        randomGenerator.nextBytes(byteArray);
        for (i = 0; i < byteArray.length; i++) {
            if (byteArray[i] < 0)
                byteArray[i] *= -1;

            while (!((byteArray[i] >= 65 && byteArray[i] <= 90) || (byteArray[i] >= 97 && byteArray[i] <= 122)
                    || (byteArray[i] <= 57 && byteArray[i] >= 48))) {

                if (byteArray[i] > 122)
                    byteArray[i] -= randomGenerator.nextInt(byteArray[i]);
                if (byteArray[i] < 48)
                    byteArray[i] += randomGenerator.nextInt(5);
                else
                    byteArray[i] += randomGenerator.nextInt(10);
            }
        }
    }
}

Related

  1. decimal_random()
  2. equalsRandomIterable(List a, List bl)
  3. extractRandomObject(Collection c)
  4. extractRandomVector(Collection c, int newSize)
  5. gaussianInt(Random random, int min, int max)
  6. generateRandom(int length, int type)
  7. generateRandom(Integer bitNum)
  8. generateRandomIntegerId()
  9. generateRandomNo()