java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

提供一个随机类

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56578
/*
* @author talent_marquis
* Email: talent_marquis@163.com
* Copyright (C) 2007 talent_marquis
* All rights reserved.
*/
package com.dextrys.trilogy.util;

import java.util.Arrays;

import org.eclipse.swt.graphics.RGB;

public class RandomUtil
{

   
/**
     *
@param args
    
*/
   
public static void main( String[] args )
    {
       
//System.out.println( getRandomNormalString( 8 ) );
        int[] test = getRandomIntWithoutReduplicate( 0, 40, 39 );
        Arrays.sort( test );
       
for( int i : test )
        {
            System.out.println( i );
        }
    }

   
/**
     * get a integer array filled with random integer without reduplicate [min, max)
     *
@param min    the minimum value
     *
@param max the maximum value
     *
@param size the capacity of the array
     *
@return a integer array filled with random integer without redupulicate
    
*/
   
public static int[] getRandomIntWithoutReduplicate( int min, int max, int size )
    {
       
int[] result = new int[size];
       
       
int arraySize = max - min;
       
int[] intArray = new int[arraySize];
       
// init intArray
        for( int i = 0 ; i < intArray.length ; i++ )
        {
            intArray[i]
= i + min;
        }
       
// get randome interger without reduplicate
        for( int i = 0 ; i < size ; i++ )
        {
           
int c = getRandomInt( min, max - i );
           
int index = c - min;
            swap( intArray, index, arraySize
- 1 - i );
            result[i]
= intArray[ arraySize - 1 - i ];
        }
               
       
return result;
    }
   
   
private static void swap( int[] array, int x, int y )
    {
       
int temp = array[x];
        array[x]
= array[y];
        array[y]
= temp;
    }
   
   
/**
     * get a random Integer with the range [min, max)
     *
@param min the minimum value
     *
@param max the maximum value
     *
@return the random Integer value
    
*/
   
public static int getRandomInt( int min, int max )
    {
       
// include min, exclude max
        int result = min + new Double( Math.random() * ( max - min ) ).intValue();

       
return result;
    }

   
/**
     * get a random double with the range [min, max)
     *
@param min the minimum value
     *
@param max the maximum value
     *
@return the random double value
    
*/
   
public static double getRandomDouble( double min, double max )
    {
       
// include min, exclude max
        double result = min + ( Math.random() * ( max - min ) );
       
return result;
    }

   
/**
     *
     *
@return a random char with the range ASCII 33(!) to ASCII 126(~)
    
*/
   
public static char getRandomChar()
    {
       
// from ASCII code 33 to ASCII code 126
        int firstChar = 33; // "!"
        int lastChar = 126; // "~"
        char result = ( char ) ( getRandomInt( firstChar, lastChar + 1 ) );
       
return result;
    }
   
   
/**
     *
     *
@return a random rgb color
    
*/
   
public static RGB getRandomRGB()
    {
       
int red = getRandomInt(0,256);
       
int green = getRandomInt(0,256);
       
int blue = getRandomInt(0,256);
       
       
return new RGB( red, green, blue );
    }
   
   
/**
     *
     *
@return a random char with the range [0-9],[a-z],[A-Z]
    
*/
   
public static char getRandomNormalChar()
    {
       
// include 0-9,a-z,A-Z
        int number = getRandomInt( 0, 62 );
       
int zeroChar = 48;
       
int nineChar = 57;
       
int aChar = 97;
       
int zChar = 122;
       
int AChar = 65;
       
int ZChar = 90;
       
       
char result;
       
       
if( number < 10 )
        {
            result
=  ( char ) ( getRandomInt( zeroChar, nineChar + 1 ) );
           
return result;

        }
       
else if( number >= 10 && number < 36 )
        {
            result
= ( char ) ( getRandomInt( AChar, ZChar + 1 ) );
           
return result;
        }
       
else if( number >= 36 && number < 62 )
        {
            result
=  ( char ) ( getRandomInt( aChar, zChar + 1 ) );
           
return result;
        }
       
else
        {
           
return 0;
        }
    }

   
/**
     *
     *
@param length the length of the String
     *
@return a String filled with random char
    
*/
   
public static String getRandomString( int length )
    {
       
// include ASCII code from 33 to 126
        StringBuffer result = new StringBuffer();
       
for( int i = 0; i < length; i++ )
        {
            result.append( getRandomChar() );
        }
       
return result.toString();
    }
   
   
/**
     *    
     *
@param length the length of the String
     *
@return a String filled with normal random char
    
*/
   
public static String getRandomNormalString( int length )
    {
       
// include 0-9,a-z,A-Z
        StringBuffer result = new StringBuffer();
       
for( int i = 0; i < length; i++)
        {
            result.append( getRandomNormalChar() );
        }
       
return result.toString();
    }
}

posted on 2009-06-10 21:23 rrong_m 阅读(90) 评论(0)  编辑  收藏

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜