随机类

本文介绍了一个实用的Java工具类,用于生成各种类型的随机数,包括整数、浮点数、字符及颜色等,并提供了一个去除重复项的随机整数数组生成方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* @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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值