Java Random String randomNumberString(int in)

Here you can find the source of randomNumberString(int in)

Description

{ method

License

Apache License

Parameter

Parameter Description
in - positive int

Return

- the string length 1..256 }

Declaration

public static String randomNumberString(int in) 

Method Source Code

//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());
    }
}

Related

  1. randomFixedByteLengthUnicodeString(Random r, int length)
  2. randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)
  3. randomize(String[] orderedList)
  4. randomizeMap(Map map)
  5. randomlyRecaseCodePoints(Random random, String str)
  6. randomRealisticUnicodeString(Random r)
  7. randomString()
  8. randomString()
  9. randomString(final int length)