Java Random randomDigit()

Here you can find the source of randomDigit()

Description

choose a random digit

License

Apache License

Return

'0' ...'9'

Declaration

public static char randomDigit() 

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

    /**//from w  w  w.  j  ava 2 s  . c o  m
     * choose a random digit
     * @return '0' ...'9'
     */
    public static char randomDigit() {
        return ((char) ('0' + randomInt(10)));
    }

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

Related

  1. randomCharacterVector(int size)
  2. RandomCode()
  3. randomCode()
  4. randomCSeq()
  5. randomData(Random random, int length)
  6. randomDoubleArray(int size)
  7. randomElement(List list, Random random)
  8. randomElement(T[] items)
  9. randomElementFromArray(T[] source)