C # typical usage of random Functions random,

C # typical usage of random Functions random ()

Random. Next () returns a non-negative Random number;

Random. Next (Int) returns a non-negative Random number less than the specified maximum.

Random. Next (Int, Int) returns a Random number within the specified range.

1. Introduction to the random (number) Function

See the help document. For more information, random (number) returns a value ranging from 0 ~ A random integer between numbers and 1. The number parameter represents an integer.

Example:

Trace (random (5 ));

2. Math. random ()

See the help documentation. Returns a zero-to-zero value with 14-bit precision ~ The number between 1. Note that there are no parameters.

Example:

Trace (Math. random ());

3. Custom Functions

The random number we sometimes need is not that simple.

For example, we want to return a random number with two decimal places, a random number between two numbers, a random letter number, and multiple random numbers,

We need to write functions to implement these functions. The following**Website source code**You can directly copy it to the first frame of the main scenario and call it. Note that some functions require entry parameters.

#1: returns a random number with a total of n digits, where m is a decimal number.

Function randomXiao (n, m ){

Var a = Math. pow (10, n + m );

Var B = random ();

Return B = B/Math. pow (10, m );

}

You can use trace (randomXiao (3, 2) to perform an experiment. This function is simple. Math. pow (n, m) is used to return a number with n as the base and m as the index. Multiplication party!

#2: returns a random number between n and m.

Function randomNm (n, m ){

If (m> = n ){

Return random (m-n + 1) + n;

}

Else {

Return false;

}

}

Random (m-n + 1) is used because the random number range is m-n, and 1 is added so that m can be in it. Add n to ensure that the random number n is the lower limit.

Add judgment to make the function more complete. In addition, if you want to return a negative random number, you can also use randomNm (n, 0); of course, I would like to use-random (n );

#3: return a letter

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 letters, remove the if clause. If you want to return lowercase letters, you can change the condition sentence to a permanent one, or remove the condition. The last sentence is changed:

Return c. toLowerCase (); String. fromCharCode (number) function returns the ASCII code of the number.

ToLowerCase () is used to convert uppercase letters into lowercase letters.

#5: Return k random numbers from n to m

Private void RandomKDiffer (int n, int m, int k, int [] arrayK ){

Int I = 0;

Int a, j;

Random random = new Random ();

While (I <k ){

A = random. Next (m-n + 1) + n;

For (j = 0; j <I; j ++ ){

If (a = arrayK [j]) {

Break;

}

}

If (j = I ){

ArrayK [I] =;

I ++;

}

}

}

The element in the array arrayK is the obtained value. Note that we borrowed random (m-n + 1) + n to return a n ~ Random Number of m. Therefore, m itself will also be returned.

If you want to return a number less than m, you can change the n value to 0. If you want to return an uncertain number at random, you can assign k = random (m-n) to the k value of the entry parameter );

Random return is not necessarily a different number. Just remove the judgment. Note that I ++ should not be omitted. It is not provided here.

# Specify several characters/numbers, and then return one or more random characters/numbers from them. You can assign the original character to an array and use the subscript of the array.

Determines the return value. We will not list functions here. You can try them by yourself.

# It should be noted that mcColor. setRBG (random (0 xFFFFFF) is usually used for randomly setting the color value of a MC.

If you want to specify a color gamut, you can use the above function. If you do not know much about the Color object, you can check it for help. We will not discuss it here.

The above functions are directly derived from random. Here is another example, which can be called derivative functions of derivative functions. The above functions are directly used. Please note that.

#6: return a random capital English string of the specified length

Function randomString (n ){

Var arrayA = randomKdiffer (1, 26, n );

Var arrayB = "";

For (var I = 0; I <n; I ++ ){

C = String. fromCharCode (arrayA [I] + 64 );

/* If (random (2 )){

C = c. toLowerCase ();

}

*/

ArrayB = arrayB + c;

}

Return arrayB;

}

Note that the StringCharCode method writes the return value in lower case as arrayB. toLowerCase (). If a case-insensitive string is returned,

Remove the comment. If you want to return a string with no specified length, you can assign the entry parameter to random (n). In this way, only the upper limit is specified. This function can also be used

The randomAscii function is implemented for everyone to think about.

#7: select random numbers in several regions

For example ~ 20,45 ~ Select a random number between the two segments. Because the number of regions is not fixed, it is inconvenient to write a function directly with a definite function,

The method we want to use is to use the switch statement for targeting. Specifically, we provide a function that returns a 1 ~ 20,45 ~ The number within 70. You can change the value in other regions.

Function randomArea (){

Var a = random (2 );

Switch (){

Case 0:

Return randomNm (1, 20); break;

Case 1:

Return randomNm (45, 70); break;

}

}

Note: We did not write the entry parameter, but directly determined in the function that it is the number of two segments, and the range is also determined. If it is three paragraphs, change it to a = random (3 );

Add a case. Of course, you can also set the range of the segment number as the entry parameter, so we will not give an example here. However, this may increase the number of parameters,

I personally do not like a function that requires many parameters. Similarly, we can return a random number of letters, letters, and numbers.

The method is only a combination of the previous functions. For example, a random letter of the specified upper-case letter is returned.

Remind me that the ASCII code for lower-case letters is ~ Z corresponds to 97 ~ 122.

Function randomAArea (a, B ){

If (ord (a) <= ord (B) & 65 <= ord (a) & ord (B) <= 90 ){

Return String. fromCharCode (randomNm (ord (a), ord (B )));

} Else {

Return false;

}

}

One function ord (char) is used, which is not recommended. It is used to return the ASCII code of char characters.

If you want to call a function anywhere, you need to change the function we write to a global function. In this way, you can freely call the system without specifying the path.

The function is the same. The method is as follows. For example, to declare the function randomXiao as a global function, you need to change the first line:

_ Global. randomXiao = function (n, m ){

// Statment

}

Friends who are not clear about the concept of global functions do not need to be intimidated by this term.

In this way, after the first line of the function is changed, it can be directly used anywhere, for example, in a MC (right, directly used, without adding the _ root path) randomXiao (n, m) you can.