A class for obtaining random numbers

Source: Internet
Author: User

Package com. Wen;

Import java. util .*;
Import java. Security. securerandom;
Import java. Security. nosuchalgorithmexception;
Import java. Security. nosuchproviderexception;

/**
* A class to obtain random numbers
* About use: We can get a random number in the 0-9 A-Z A-Z.
* Randomstr. setcharset ("0-9"); // range
* Randomstr. setlength ("8"); // Length
* Randomstr. generaterandomobject ();
* Random number (random type) = randomstr. getrandom ();
* <P> copyright: 2004 </P>
* @ Author assumerwg
* @ Version 1.1
*/

Public class randomstr {
Static string randomstr;
Static Boolean allchars = false;
// The missing string is an 8-Bit String.
Static integer length = new INTEGER (8 );
Static hashmap hmap;
Static arraylist lower = NULL;
Static arraylist upper = NULL;
Static char [] single = NULL;
Static int singlecount = 0;
Static Boolean singles = false;
Static string algorithm = NULL;
Static string provider = NULL;
Static Boolean secure = false;
Static random = NULL;
Static securerandom secrandom = NULL;

Private Static float getfloat (){
If (random = NULL ){
Return secrandom. nextfloat ();
}
Else {
Return random. nextfloat ();
}
}

Public static void generaterandomobject () throws exception {
// Check to see if the object is a securerandom object
If (secure ){
Try {
// Get an instance of a securerandom object
If (provider! = NULL ){

Random = securerandom. getinstance (algorithm, provider );
}
Else {
Random = securerandom. getinstance (algorithm );
}
}
Catch (nosuchalgorithmexception ne ){
Throw new exception (ne. getmessage ());
}
Catch (nosuchproviderexception PE ){
Throw new exception (PE. getmessage ());
}
}
Else {
Random = new random ();
}
}

/**
* Generate the random string
*/
Private Static void generaterandom (){
If (allchars ){
For (INT I = 0; I <length. intvalue (); I ++ ){
Randomstr = randomstr +
New character (char) (INT) 34 + (INT) (getfloat () * 93 )))).
Tostring ();
}
}
Else if (Singles ){
// Check if there are single chars to be encoded DED
If (upper. Size () = 3 ){
// Check for the number of ranges Max 3 uppercase lowercase digits build the random string
For (INT I = 0; I <length. intvalue (); I ++ ){
// You have four groups to choose a random number from, to make
// The choice a little more random select a number out of 100 get a random number even or odd
If (INT) (getfloat () * 100) % 2 = 0 ){
// The number was even get another number even or odd
If (INT) (getfloat () * 100) % 2 = 0 ){
// Choose a random char from the single char Group
Randomstr = randomstr + randomsingle (). tostring ();
}
Else {
// Get a random char from the first range
Randomstr = randomstr +
Randomchar (character) lower. Get (2), (character) Upper. Get (2 )).
Tostring ();
}
}
Else {
// The number was odd

If (INT) (getfloat () * 100) % 2 = 0 ){
// Choose a random char from the second range
Randomstr = randomstr +
Randomchar (character) lower. Get (1), (character) Upper. Get (1 )).
Tostring ();
}
Else {
// Choose a random char from the third range
Randomstr = randomstr +
Randomchar (character) lower. Get (0), (character) Upper. Get (0 )).
Tostring ();
}
}
}
}
Else if (upper. Size () = 2 ){
// Single chars are to be encoded ded choose a random char from
// Two different ranges build the random char from single chars and two ranges
For (INT I = 0; I <length. intvalue (); I ++ ){
// Select the single chars or a range to get each random charfrom

If (INT) (getfloat () * 100) % 2 = 0 ){
// Get random char from the single chars
Randomstr = randomstr + randomsingle (). tostring ();
}
Else if (INT) (getfloat () * 100) % 2 = 0 ){
// Get the random char from the first range
Randomstr = randomstr +
Randomchar (character) lower. Get (1), (character) Upper. Get (1 )).
Tostring ();
}
Else {
// Get the random char from the second range
Randomstr = randomstr +
Randomchar (character) lower. Get (0), (character) Upper. Get (0 )).
Tostring ();
}
}
}
Else if (upper. Size () = 1 ){
// Build the random string from single chars and one range
For (INT I = 0; I <length. intvalue (); I ++ ){
If (INT) getfloat () * 100) % 2 = 0 ){
// Get a random single char
Randomstr = randomstr + randomsingle (). tostring ();
}
Else {
// Get a random char from the range
Randomstr = randomstr +
Randomchar (character) lower. Get (0), (character) Upper. Get (0 )).
Tostring ();
}
}
}
Else {
// Build the Rand string from single chars
For (INT I = 0; I <length. intvalue (); I ++ ){
Randomstr = randomstr + randomsingle (). tostring ();
}
}
}
Else {
// No single chars are to be encoded in the random string
If (upper. Size () = 3 ){
// Build random strng from three ranges
For (INT I = 0; I <length. intvalue (); I ++ ){
If (INT) (getfloat () * 100) % 2 = 0 ){
// Get random char from first range
Randomstr = randomstr +
Randomchar (character) lower. Get (2), (character) Upper. Get (2 )).
Tostring ();
}
Else if (INT) (getfloat () * 100) % 2 = 0 ){
// Get random char form second range
Randomstr = randomstr +
Randomchar (character) lower. Get (1), (character) Upper. Get (1 )).
Tostring ();
}
Else {
// Get random char from third range
Randomstr = randomstr +
Randomchar (character) lower. Get (0), (character) Upper. Get (0 )).
Tostring ();
}
}
}
Else if (upper. Size () = 2 ){
// Build random string from two ranges
For (INT I = 0; I <length. intvalue (); I ++ ){
If (INT) (getfloat () * 100) % 2 = 0 ){
// Get random char from first range
Randomstr = randomstr +
Randomchar (character) lower. Get (1), (character) Upper. Get (1 )).
Tostring ();
}
Else {
// Get random char from second range
Randomstr = randomstr +
Randomchar (character) lower. Get (0), (character) Upper. Get (0 )).
Tostring ();
}
}
}
Else {
// Build random string
For (INT I = 0; I <length. intvalue (); I ++ ){
// Get random char from only range
Randomstr = randomstr +
Randomchar (character) lower. Get (0), (character) Upper. Get (0 )).
Tostring ();
}
}
}
}

/**
* Generate a random char from the single char list
*
* @ Returns-A randomly selscted character from the single char list
*
*/
Private Static character randomsingle (){
Return (new character (single [(INT) (getfloat () * singlecount)-1)]);
}

/**
* Generate a random character
*
* @ Param lower bound from which to get a random char
* @ Param upper bound from which to get a random char
*
* @ Returns-A randomly generated character
*
*/
Private Static character randomchar (character lower, character upper ){
Int tempval;
Char low = lower. charvalue ();
Char up = upper. charvalue ();

// Get a random number in the range lowlow-lowup
Tempval = (INT) Low + (getfloat () * (INT) (up-low ))));

// Return the random char
Return (new character (char) tempval ));
}

/**
* Get the randomly created string for use with
* & Lt; JSP: getproperty name = <I> "ID" </I> property = "randomstr"/& gt;
*
* @ Return-randomly created string
*
*/
Public static string getrandom (){
Randomstr = new string ();
Generaterandom (); // generate the first random string

If (hmap! = NULL ){

While (hmap. containskey (randomstr )){
// Random string has already been created generate a different one
Generaterandom ();
}

Hmap. Put (randomstr, null); // Add the new random string
}

Return randomstr;
}

/**
* Set the ranges from which to choose the characters for the random string
* @ Param low set of lower ranges
* @ Param up set of upper ranges
*
*/
Public static void setranges (arraylist low, arraylist up ){
Lower = low;
Upper = up;
}

/**
* Set the hashmap that is used to check the uniqueness of random strings
* @ Param map hashmap whose keys are used to insure uniqueness of random strgs
*/
Public static void sethmap (hashmap map ){
Hmap = map;
}

/**
* Set the length of the random string
* @ Param value length of the random string
*/
Public static void setlength (string value ){
Length = new INTEGER (value );
}
Public static void setlength (INT value ){
Length = new INTEGER (value );
}
/**
* Set the algorithm name
* @ Param Value Name of the algorithm to use for a securerandom object
*/
Public static void setalgorithm (string value ){
Algorithm = value;
Secure = true; // A securerandom object is to be used
}

/**
* Set the provider name
* @ Param Value Name of the package to check for the algorithm
*/
Public static void setprovider (string value ){
Provider = value;
}

/**
* Set the allchars flag
* @ Param value Boolean value of the allchars flag
*/
Public static void setallchars (Boolean value ){
Allchars = value;
}

/**
* Set the array of single chars to choose from for this random string and
* Number of chars in the array
* @ Param chars the array of single chars
* @ Param value the number of single chars
*
*/
Public static void setsingle (char [] chars, int value ){
Single = chars; // set the array of chars
Singlecount = value; // set the number of chars in array single
Singles = true; // set flag that single chars are in use
}

Public static void setcharset (string value ){
// Values tells the method whether or not to check for single chars
Boolean more = true;

// Create the arraylists to hold the upper and lower bounds for the char
// Ranges
Lower = new arraylist (3 );
Upper = new arraylist (3 );

// User has chosen to use all possible characters in the random string
If (value. compareto ("all") = 0 ){
Allchars = true; // set allchars flag
// All chars are to be used so there are no single chars to sort
// Through
More = false;
}
Else if (value. charat (1) = '-') & (value. charat (0 )! = '//')){
// Run through the ranges at most 3
While (more & (value. charat (1) = '-')){
// Check to make sure that the dash is not the single char
If (value. charat (0) = '//'){
Break;
}
Else {
// Add upper and lower ranges to there list
Lower. Add (new character (value. charat (0 )));
Upper. Add (new character (value. charat (2 )));
}

// Check to see if there is more to the charset
If (value. Length () <= 3 ){
More = false;
}
Else {
// Create a new string so that the next range if there is one
// Starts it
Value = value. substring (3 );
}
}
}

// If more = false there are no single chars in the charset
If (more ){
Single = new char [30]; // create single

// Create a set of tokens from the string of single chars
Stringtokenizer tokens = new stringtokenizer (value );

While (tokens. hasmoretokens ()){
// Get the next token from the string
String token = tokens. nexttoken ();

If (token. Length ()> 1 ){
// Char is a-add it to the list
Single [singlecount ++] = '-';
}

// Add the current char to the list
Single [singlecount ++] = token. charat (0 );
}
}
If (lower = NULL) & (single = NULL )){
Setcharset ("a-zA-Z0-9 ");
}
}
/*
Public static void main (string [] ARGs ){
Try {
// Randomstr. setallchars (true );
Randomstr. setcharset ("0-9 ");
Randomstr. setlength (12 );
Randomstr. generaterandomobject ();
System. Out. println (randomstr. getrandom ());
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
*/
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.