📄 getrandomnumber.java
字号:
package randonNumber;
public class GetRandomNumber {
/**
* java.util.Random rd = new java.util.Random();
rd.nextInt(); //get a unlimited integer, it can be positive or negative.
rd.netxInt(max); //get a unnegative interger between 0(included) an max(unincluded)
rd.nextDouble();//get a double between 0.0d(included) and 1.0d(unincluded)
in java sdk, there is no GetRandomString()
after new String(), there is a String, but it's empty, not null
System.out.println(new String()); ==== SPACE
*
*
*/
//-----------------------------------------------------------------
public static int GetRandomInteger()
{
java.util.Random rd = new java.util.Random();
return rd.nextInt();
}
//-----------------------------------------------------------------
public static int GetRandomInteger(int bound)
{
java.util.Random rd = new java.util.Random();
return rd.nextInt(bound);
}
//-----------------------------------------------------------------
public static int GetRandomPositiveInteger(int bound)
{
java.util.Random rd = new java.util.Random();
int rn;
do
{
rn = rd.nextInt(bound);
}while(rn == 0);
return rn;
}
//-----------------------------------------------------------------
public static double GetRanomDouble()
{//the double is between 0.0d(include) and 1.0d(not include)
java.util.Random rd = new java.util.Random();
return rd.nextDouble();
}
//-----------------------------------------------------------------
public static String GetRandomStringUnstrict()
{ //get a String, may be null, may be " ";
int maxlength = 16;
String str = null;
char [] source = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z', //0 - 25
'A','B','C','D','E','F','G','H','I','J','K','L','M','N', //26 - 51
'O','P','Q','R','S','T','U','V','W','X','Y','Z', //52 - 77
'0','1','2','3','4','5','6','7','8','9', //78 - 87
'@','#','$','%',//88-91
' '}; //92 SPACE, which means SPACE is the source.length_th char
int len = GetRandomInteger(maxlength); //len can be 0
for(int i = 0; i < len; i++)
{
str += source[GetRandomInteger(source.length)];//source.length - maxIndexofSource[] = 1,char can be SPACE
}
return str;
}
//-----------------------------------------------------------------
public static String GetRandomString()
{ //get a String, may be null, may be " ";
int maxlength = 16;
String str = new String();
char [] source = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z', //0 - 25
'A','B','C','D','E','F','G','H','I','J','K','L','M','N', //26 - 51
'O','P','Q','R','S','T','U','V','W','X','Y','Z', //52 - 77
'0','1','2','3','4','5','6','7','8','9', //78 - 87
'@','#','$','%',//88-91
' '}; //92 SPACE, which means SPACE is the source.length_th char
int len = GetRandomInteger(maxlength) + 1; //len can't be 0
for(int i = 0; i < len; i++)
{
str += source[GetRandomInteger(source.length - 1)];//char[i] can'T be SAPCE
}
return str;
}
//-----------------------------------------------------------------
public static String GetRandomString(int len)
{ //get a String, can't be null or " ";
String str = new String();
char [] source = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z', //0 - 25
'A','B','C','D','E','F','G','H','I','J','K','L','M','N', //26 - 51
'O','P','Q','R','S','T','U','V','W','X','Y','Z', //52 - 77
'0','1','2','3','4','5','6','7','8','9', //78 - 87
'@','#','$','%',//88-91
' '}; //92 SPACE, which means SPACE is the source.length_th char
for(int i = 0; i < len; i++)
{
str += source[GetRandomInteger(source.length - 1)];//char[i] can'T be SAPCE
}
return str;
}
//-----------------------------------------------------------------
public static void Output(Object obj)
{
System.out.println(obj.getClass().getName()+ " : "+obj);
}
//-----------------------------------------------------------------
public static void main(String[] args)
{
java.util.Random rd = new java.util.Random();
for(int i = 0; i < 100; i++)
{
System.out.println(rd.nextInt());
}
Output(GetRandomInteger());
Output(GetRandomPositiveInteger(15));
Output(GetRanomDouble());
Output(GetRandomString());
Output(GetRandomString(15));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -