public static long getRandomUID ( final int width ) { if ( width > MAX_WIDTH ) { throw new IllegalArgumentException ( "Expecting to return an unsigned long " + "random integer, it can not be larger than " + MAX_WIDTH + " bytes wide" ) ; } final byte [ ] bytes = new byte [ width ] ; random_generator . nextBytes ( bytes ) ; long value = 0 ; for ( int i = 0 ; i < bytes . length ; i ++ ) { value <<= 8 ; value |= bytes [ i ] & 0xFF ; } // make sure we never return 0 as a UID return value != 0 ? value : value + 1 ; }
Get the next random UID . It creates random bytes then convert it to an unsigned long .