C # Random generation of random numbers and the same random number solution

Source: Internet
Author: User
Tags datetime lowercase pow random seed

The first method does not specify a random seed, and the system automatically selects the current time as a random seed:

The code is as follows Copy Code

Random ro = new Random ();

The second method can specify an int type parameter as a random seed:

The code is as follows Copy Code

int iseed=10;
Random ro = new Random (10);
Long tick = DateTime.Now.Ticks;
Random ran = new Random ((int) (tick & 0xffffffffL) | (int) (tick >> 32));

This will ensure that 99% is not the same.


Short time repetition

2. Generation of non-repetitive random numbers using seeds

(a) when generating random numbers: Random ran = new Random ((int) DateTime.Now.Ticks);

The code is as follows Copy Code

Ran. Next (Minvale, Maxvale);

Can effectively solve the problem of duplication

Number of ticks PS:DateTime.Now.Ticks as data number

(b) Random Random = new Random (Getrandomseed ())//implementation using cryptographic service provider (CSP) to implement the cryptographic random number generator (RNG) s

The code is as follows Copy Code

tatic int Getrandomseed () {

byte[] bytes = new Byte[4];

System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider (); Rng. GetBytes (bytes);

Return Bitconverter.toint32 (bytes, 0);

}

or Random Sourcegen = new Random (new Guid (). GetHashCode ());//Using GUID

3. Use the time delay to solve the repetitive problem

Thread.Sleep (100);

4. Recursion, using it to detect whether there is duplication of generated random numbers, if taken out of the number and the number has been obtained repeated random access.

The code is as follows Copy Code

Random ra=new Random (Unchecked ((int) DateTime.Now.Ticks));
Int[] Arrnum=new int[10];
int tmp=0;
int minvalue=1;
int maxvalue=10;
for (int i=0;i<10;i++)
{
Tmp=ra. Next (Minvalue,maxvalue); Random Fetch number
Arrnum=getnum (Arrnum,tmp,minvalue,maxvalue,ra); Take out values to the array
}
.........
.........
public int getnum (int[] arrnum,int tmp,int minvalue,int maxvalue,random ra)
{
int n=0;
while (n<=arrnum.length-1)
{
if (arrnum[n]==tmp)//Use loops to determine if there is duplication
{
Tmp=ra. Next (Minvalue,maxvalue); Re-randomly acquired.
Getnum (Arrnum,tmp,minvalue,maxvalue,ra)//recursive: If the number taken out and the number obtained have been repeated randomly retrieved.
}
n++;
}
return TMP;
}

Some other references

1: Returns a total of n digits, where m is the random number of decimal numbers

The code is as follows Copy Code


--------------------------------------------------------------------------------

function Randomxiao (n,m) {
var a = Math.pow (n+m);
var B = random (a);
Return B=b/math.pow (m);
}


--------------------------------------------------------------------------------

You can use Trace (Randomxiao (3,2)); This function is simple. Math.pow (n,m) is used to return a number with N as the base and M as exponent. Squares

# 2: Returns a random number between N and M

The code is as follows Copy Code


--------------------------------------------------------------------------------

function Randomnm (n,m) {
if (m>=n) {
return random (m-n+1) +n;
}
else {
return false;
}
}


--------------------------------------------------------------------------------

The reason for using random (m-n+1) is because the range of random numbers is m-n, plus 1 allows m to be inside. Plus n guarantees that the random number is lower than N.
Plus judgment makes the function more complete. In addition, if you want to return a negative number of random numbers, you can also use RANDOMNM (n,0); Of course, I think more generally with-random (n);

# 3: Returns a letter

The code is as follows Copy Code


--------------------------------------------------------------------------------

function Randomascii () {
var c = String.fromCharCode (Random (26) +65);
if (random (2)) {
return C.tolowercase ();
}
return C;
}


--------------------------------------------------------------------------------

#4: Returns a case-insensitive random letter

If you want to return uppercase, you can remove the if condition sentence. If you want to return to lowercase, you can change the conditional sentence to permanent, or remove the condition, the last sentence should read:
return C.tolowercase (); The String.fromCharCode (number) function returns the ASCII code of numbers that represents the digits.
toLowerCase () is used to convert uppercase letters to lowercase.

# 5: Returns a K-reciprocal random number between N and M

The code is as follows Copy Code


function Randomkdiffer (n,m,k) {
Arrayk = [];
var i = 0;
while (I < k) {
A = random (m-n+1) +n;
for (var j = 0; J < i; J + +) {
if (a = = Arrayk[j]) {
Break
}
}
if (j = = i) {
Arrayk[i] = A;
i++;
}
}
return Arrayk;
}

Related Article

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.