*
* @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();
}
}
分享到:
相关推荐
java中随机函数的使用 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1)) 例: (int)(1+Math.random()*(10-1+1)) 从1到10的int型随数方法2 获得随机数
java开发技术调用rendom函数,随机生成32位不重复的字符
java中随机函数的使用.doc
这是一个通过随机数产生人名的函数,姓从百家姓中随机抽取,名在常用名中抽取
java源码:利用随机函数抽取幸运数字.zip
此为一个Java的随机组卷程序课程设计,内有完整程序和论文,实现的功能有: 1、卷子内容可以任意专业内容。四项单选题。 2. 数据源为50题,随机选择30题。 3. 要求每套题生成后,要方便打印 4. 使用外部数据。 5. ...
随机生成用户名样例: 339063 77188 fengjr1980@yeah.net 97133 xupe19870102@sohu.com 18955990722 2145028 25070167 299966 13329809029 18739552501 77223 725525865 cheng1991 chub1974 13290062609 wangf...
java试验,random函数调用等问题。基础编程。
利用随机函数Math.random()产生火 柴数量(20~50), 每次最多拿3根,拿到最后一根为胜者。 要求:游戏过程要显示火柴总数,选择谁先拿;每步要显示剩余火柴数量,以及计算机拿的数量,并提示用户 输入拿多少;...
用KFCM函数进行随机分类的代码.zip
这2个函数实现了随机选择数据库中的题目,只需要读入界面端的每章题目数量,并按顺序排列,则可以从数据库中选择出所需要的题目的ID
利用随机函数抽取幸运数字
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件 4个...
HelloNative.java 准备调用C函数的java文件 HelloNative.lib 用VC编译生成的静态库文件 HelloNative.obj 用VB编译生成的目标文件 HelloNativeTest.java 测试本地化是否成功的类文件 instanceVar.java 定义一个...
设计一个负责排序的程序包,实现多种排序算法,至少包括插入排序、冒泡排序和快速排序算法。 要求: 1.可以对任何简单类型和任意对象进行排序 2.可以支持升序、降序、字典排序等多种顺序要求 3.可以随意增加排序算法...
产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1的随机数产生0或1...
java考试参考题:利用随机函数定义10对(x,y)值,由此创建的Point类实例存入一个数组中,按与原点(0,0)的距离由小到大的顺序输出所有的点及到原点的距离。鼠标 ,键盘事件等
java字符串实验题目 java string类
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件...
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件 4个...