returns a random number among { 0 to count-1 } : Random « Development « Android






returns a random number among { 0 to count-1 }

    
import java.util.Calendar;
import java.util.Random;

class CommonUtil {
  
  private static Random random = new Random(Calendar.getInstance().getTimeInMillis());
  
  /**
   * 
   * @param count
   * @return returns a random number among { 0 to count-1 }
   */
  public static short getRandomIndex(short count){
    int randomIndex = random.nextInt();
    short resultIndex = (short) (randomIndex % count);
    if(resultIndex < 0){
      resultIndex += count;
    }
    return resultIndex;
  }

}

   
    
    
    
  








Related examples in the same category

1.Random Alphabet Maker
2.Next random Int
3.Random Xor Obfuscator
4.Random Generator
5.Get Random Index