FileDocCategorySizeDatePackage
RandomUtils.javaAPI DocAzureus 3.0.3.44274Thu Jan 11 10:18:16 GMT 2007org.gudy.azureus2.core3.util

RandomUtils

public class RandomUtils extends Object
author
MjrTom 2006/Jan/02: added various methods, including some java.util.Random method aliases

Fields Summary
public static final Random
RANDOM
public static final int
LISTEN_PORT_MIN
public static final int
LISTEN_PORT_MAX
Constructors Summary
Methods Summary
public static java.lang.StringgenerateRandomAlphanumerics(int num_to_generate)
Generate a random string of charactors.

param
num_to_generate number of chars to generate
return
random char string

		String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		
		StringBuffer buff = new StringBuffer( num_to_generate );
		
    for( int i=0; i < num_to_generate; i++ ) {
    	int pos = (int)( RANDOM.nextDouble() * alphabet.length() );
    	buff.append( alphabet.charAt( pos ) );
    }
    
    return buff.toString();
  
public static byte[]generateRandomBytes(int num_to_generate)
Generate a random array of bytes.

param
num_to_generate number of bytes to generate
return
random byte array

	
	
	                 	 
	       
    byte[] id = new byte[ num_to_generate ];
    RANDOM.nextBytes( id );    
    return id;
  
public static intgenerateRandomIntBetween(int min, int max)

return
random int between min and max, e.g params of [5,7] returns 5,6 or 7

		return min +generateRandomIntUpto(max + 1 - min);
	
public static intgenerateRandomIntUpto(int max)

return
random int between 0 and max-1. e.g. param of 10 returns 0->9

		return nextInt(max);
	
public static intgenerateRandomNetworkListenPort()
Generate a random port number for binding a network IP listening socket to. NOTE: Will return a valid non-privileged port number >= LISTEN_PORT_MIN and <= LISTEN_PORT_MAX.

return
random port number

	
	
	                              	 
	   
	 
	
			// DON'T use NetworkManager methods to get the ports here else startup can hang
		
		int	existing_tcp	= COConfigurationManager.getIntParameter( "TCP.Listen.Port" );
		int existing_udp	= COConfigurationManager.getIntParameter( "UDP.Listen.Port" );
		int existing_udp2	= COConfigurationManager.getIntParameter( "UDP.NonData.Listen.Port" );
		
		while( true ){
			int min 	= LISTEN_PORT_MIN;
			int port 	= min + RANDOM.nextInt( LISTEN_PORT_MAX + 1 - min );
			
				// skip magnet ports
			
			if ( port >= 45100 && port <= 45110 ){
				
				continue;
			}
			
			if ( port != existing_tcp && port != existing_udp && port != existing_udp2){
				
				return port;
			}
		}
	
public static intgenerateRandomPlusMinus1()
Generates a random +1 or -1

return
+1 or -1

		return RANDOM.nextBoolean() ? -1:1;
	
public static bytenextByte()

        return (byte)RANDOM.nextInt();
    
public static voidnextBytes(byte[] bytes)

    	RANDOM.nextBytes(bytes);
	
public static floatnextFloat()

		return RANDOM.nextFloat();
	
public static intnextInt(int n)

    	return RANDOM.nextInt(n);
	
public static intnextInt()

        return RANDOM.nextInt();
    
public static longnextLong()

        return RANDOM.nextLong();