Code example for Random

Methods: nextBytes, nextInt

0
  { 
    if( max == min ) return min;
 
    int range = max - min;
    range = range > 0 ? range: -range; //Prevent integer overflow.
    int generated = min + rand.nextInt(range);
    generated = generated > max ? max: generated;
    return generated;
  } 
 
  public String getNextString()
  { 
    return getNextString(StringFieldGenerate.getMinStringLength(), StringFieldGenerate.getMaxStringLength()); 
  } 
 
  public String getNextString(int min, int max)
  {      
    int length = getNextInt(min, max);
 
    StringBuffer strbld = new StringBuffer();
    for( int i = 0; i < length; i++)
    { 
      char ch = validChars.charAt(rand.nextInt(validChars.length()));
      strbld.append(ch);
    } 
 
    return strbld.toString();
  } 
 
  public double getNextDouble() { 
    return rand.nextDouble();
  } 
 
  public float getNextFloat() { 
    return rand.nextFloat();
  } 
 
  public long getNextLong() { 
    long randomLong = rand.nextLong();
 
    return randomLong == Long.MIN_VALUE ? 0: Math.abs(randomLong);
  } 
 
  public boolean getNextBoolean() 
  { 
    return rand.nextBoolean();
  } 
 
  public  byte[] getNextBytes(int maxBytesLength)
  { 
    byte[] bytes = new byte[this.getNextInt(0, maxBytesLength)];
    rand.nextBytes(bytes);
    return bytes;
  } 
}