Java - Write code to Return a plausibly-random number string in hex form.

Requirements

Write code to Return a plausibly-random number string in hex form.

Demo

//package com.book2s;
import java.util.Random;

public class Main {
    public static void main(String[] argv) {
        System.out.println(getRandomHexString());
    }//from www  .  j  a va2  s.  co  m

    private static final Random rand = new Random(
            System.currentTimeMillis());

    /**
     * Return a plausibly-random number string in hex form.
     * Used mostly for temp filename generation.<p>
     * 
     * Not (yet) synchronised as unlikely to be problem
     * with threads and contention.
     */

    public static String getRandomHexString() {
        long n = rand.nextLong();
        if (n == Long.MIN_VALUE) {
            n = 0; // corner case
        } else {
            n = Math.abs(n);
        }

        return Long.toString(n, 16);
    }
}