In many cases, something needs to be randomly generated, and the result must be slowly used in the. NET Framework's random class. Later, I had to write a more convenient multi-function random type, named myrandom. I will share it today.
All the members of this class are static, mainly for convenient calls. Of course, if you want to generate multiple instances for it, you can remove all static keywords.
Second, this class is correct. net pre-defined random class encapsulation, so some places where the random type is not suitable, this will also be not suitable, for example, for security-intensive password random number generation, it is best to use system. security. the randomnumbergenerator type provided by the cryptography namespace.
For methods of the. NET random type, this type has corresponding support, such as providing seeds and randomly generating arrays of int, double, and byte. The following describes other more comprehensive methods provided by the myrandom class.
Directory
- String-related operations
- Array-related operations
- Enumeration operations
- Loop-related operations
- Source code download
Returned directory
String-related operations
Sample Code:
// Generate 11 digits at random
String str1 = myrandom. String (stringrange. digit, 11 );
Console. writeline (str1 );
// Generate 5 lowercase letters at random
String str2 = myrandom. String (stringrange. letter, 5). tolower ();
Console. writeline (str2 );
// Randomly generate a 10-character array from the specified string
Char [] chars = myrandom. chararray ("mgen! ", 10 );
Console. writeline (new string (chars ));
// Returns a random string.
Char c = myrandom. charfromstring ("mgen ");
Console. writeline (C );
Possible output:
76593373627
Slave DRT
Gngemgnm !!
G
Returned directory
Array-related operations
Sample Code:
Int [] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Returns a random Member of the array.
Int I1 = myrandom. arraymember (ARR );
Console. writeline (I1 );
// Randomly return a Member in the specified range of the array
Int I2 = myrandom. arraymember (ARR, 2, 3 );
Console. writeline (I2 );
// Returns a random array of the specified length based on the range of the specified array.
// The newarray1 variable has a length of 5 and all Members are random members in the ARR variable.
Int [] newarray1 = myrandom. arrayfixedsegment (ARR, 5 );
Console. writeline (string. Join (",", newarray1 ));
// Returns a random array with a dynamic Length Based on the range of the specified array.
// The newarray2 variable array length is random (within the range of 1 to 10). The array members are random among the first three members of the ARR array.
Int [] newarray2 = myrandom. arraydynamicsegment (ARR, 0, 3, 10, 1 );
Console. writeline (string. Join (",", newarray2 ));
// Randomly fill in a byte (or integer) array
Byte [] bytes = new byte [10];
Myrandom. bytearray (bytes );
Console. writeline (bitconverter. tostring (bytes ));
// Returns a random byte (or integer) array.
VaR bytes2 = myrandom. bytearray (5 );
Console. writeline (bitconverter. tostring (bytes2 ));
Possible output:
5
5
8, 3, 5, 6, 4
1, 2, 3, 3, 1, 2, 3, 2, 1, 2
3c-90-95-ab-65-8f-6b-5f-4f-1f
50-b2-a9-f4-78
Returned directory
Enumeration operations
Sample Code:
// Randomly generate enumeration values
For (INT I = 0; I <3; I ++)
{
Environment. specialfolder Sf = myrandom. Enumeration <environment. specialfolder> ();
Console. writeline (SF );
}
Possible output:
Desktopdirectory
ProgramFiles
Localapplicationdata
Returned directory
Loop-related operations
Sample Code:
// Perform 1-5 Operations randomly
Myrandom. Action () => console. writeline ("Operation 1"), 5 );
// Perform 0-3 operations randomly (with index value)
Myrandom. Action (idx) => console. writeline ("Operation 2, currently {0}", idx), 3, 0 );
// Perform 2-4 Operations randomly (with index value and total number of times)
Myrandom. action (idx, total) => console. writeline ("Operation 3, currently {0}, total {1}", idx, total), 4, 2 );
Possible output:
Operation 1
Operation 1
Operation 2, which is currently 0th
Operation 2, which is currently 1st
Operation 3: There are currently 0th, 3 in total
Operation 3: There are currently 1st, 3 in total
Operation 3: There are currently 2nd, 3 in total
Returned directory
Source code download
Download the current version of the program and source code
Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.
File Type:. CS File