Java Random Number getRandomPortNumber()

Here you can find the source of getRandomPortNumber()

Description

Returns a random local port number that user applications could bind to.

License

LGPL

Return

a random int located between 1024 and 65 535.

Declaration

public static int getRandomPortNumber() 

Method Source Code

//package com.java2s;
/*/* ww w  . jav a 2s  .  c o m*/
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.util.*;

public class Main {
    /**
     * The maximum int value that could correspond to a port number.
     */
    public static final int MAX_PORT_NUMBER = 65535;
    /**
     * The minimum int value that could correspond to a port number bindable
     * by the SIP Communicator.
     */
    public static final int MIN_PORT_NUMBER = 1024;
    /**
     * The random port number generator that we use in getRandomPortNumer()
     */
    private static Random portNumberGenerator = new Random();

    /**
     * Returns a random local port number that user applications could bind to.
     * (i.e. above 1024).
     * @return a random int located between 1024 and 65 535.
     */
    public static int getRandomPortNumber() {
        return getRandomPortNumber(MIN_PORT_NUMBER, MAX_PORT_NUMBER);
    }

    /**
     * Returns a random local port number, greater than min and lower than max.
     *
     * @param min the minimum allowed value for the returned port number.
     * @param max the maximum allowed value for the returned port number.
     *
     * @return a random int located between greater than min and lower than max.
     */
    public static int getRandomPortNumber(int min, int max) {
        return portNumberGenerator.nextInt(max - min) + min;
    }

    /**
     * Returns a random local port number, greater than min and lower than max.
     * If the pair flag is set to true, then the returned port number is
     * guaranteed to be pair. This is useful for protocols that require this
     * such as RTP
     *
     * @param min the minimum allowed value for the returned port number.
     * @param max the maximum allowed value for the returned port number.
     * @param pair specifies whether the caller would like the returned port to
     * be pair.
     *
     * @return a random int located between greater than min and lower than max.
     */
    public static int getRandomPortNumber(int min, int max, boolean pair) {
        if (pair) {
            int delta = max - min;
            delta /= 2;
            int port = getRandomPortNumber(min, min + delta);
            return port * 2;
        } else {
            return getRandomPortNumber(min, max);
        }
    }
}

Related

  1. getRandomNumberInRange(Random random, int min, int max)
  2. getRandomNumbers(int range, int count, Random rnd)
  3. getRandomNumberString(int length)
  4. getRandomNumer(int length)
  5. getRandomNumericString(int maxlen)
  6. getRandonNumber(int range)
  7. getrannumber()
  8. getRndNumByLen(int lengthOfNumber)
  9. getSerialNumber(int length)