Java Random String randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)

Here you can find the source of randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)

Description

Fills provided char[] with valid random unicode code unit sequence.

License

Apache License

Declaration

public static void randomFixedLengthUnicodeString(Random random,
        char[] chars, int offset, int length) 

Method Source Code

//package com.java2s;
/**//from   w  w  w . j  a v  a2  s . com
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 {
    /**
     * Fills provided char[] with valid random unicode code
     * unit sequence.
     */
    public static void randomFixedLengthUnicodeString(Random random,
            char[] chars, int offset, int length) {
        int i = offset;
        final int end = offset + length;
        while (i < end) {
            final int t = random.nextInt(5);
            if (0 == t && i < length - 1) {
                // Make a surrogate pair
                // High surrogate
                chars[i++] = (char) nextInt(random, 0xd800, 0xdbff);
                // Low surrogate
                chars[i++] = (char) nextInt(random, 0xdc00, 0xdfff);
            } else if (t <= 1) {
                chars[i++] = (char) random.nextInt(0x80);
            } else if (2 == t) {
                chars[i++] = (char) nextInt(random, 0x80, 0x7ff);
            } else if (3 == t) {
                chars[i++] = (char) nextInt(random, 0x800, 0xd7ff);
            } else if (4 == t) {
                chars[i++] = (char) nextInt(random, 0xe000, 0xfffe);
            }
        }
    }

    /** start and end are BOTH inclusive */
    public static int nextInt(Random r, int start, int end) {
        return start + r.nextInt(end - start + 1);
    }
}

Related

  1. getString(int length)
  2. getString(int n, int arg[])
  3. randomCharacter(String availableValues)
  4. randomFileString()
  5. randomFixedByteLengthUnicodeString(Random r, int length)
  6. randomize(String[] orderedList)
  7. randomizeMap(Map map)
  8. randomlyRecaseCodePoints(Random random, String str)
  9. randomNumberString(int in)