C # stochastic function random () Typical usage highlights

Source: Internet
Author: User

C # stochastic function random () Typical usage highlights

Random.next () Returns a non-negative random number;

Random.next (INT) returns a nonnegative random number that is less than the specified maximum value

Random.next (int,int) returns a random number in a specified range

1. Introduction to the random (number) function

See the Help document, and simply mention that random (number) returns an integer between 0~number-1. The parameter number represents an integer.
Example:
Trace (Random (5));

2, Math.random ()
See the Help documentation. Returns the number of 0~1 with a 14-bit precision, noting that there are no parameters.
Example:
Trace (Math.random ());

3. Custom Functions

We sometimes need random numbers that are not so simple.
For example, we want to return a random number with two decimal places, return a random number between two numbers, return a random number of letters, return multiple random numbers, etc.
These all need to be written in our own function. The following website source code directly copied to the main scene of the first frame can be called. Note that some functions require entry parameters.

#1: Returns a total of n digits, where the M-bit is a random number of decimals

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

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

# 2: Returns a random number from N to M

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

Random is used because the range of m-n+1 is m-n, plus 1 allows m to be inside. Plus n guarantees that the random number is at the lower limit of 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: 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, remove the if condition sentence on the line. 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 that numbers represents.
toLowerCase () is used to convert uppercase letters to lowercase.

# 5: Returns a K-xor random number between N and 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] = A;
i++;
}
}
}

The elements in the array Arrayk are the resulting values. Notice that we borrowed random (m-n+1) +n to return a n~m number. So M itself will also be returned.
If you want to return a number within m, you can change the value of N to 0. If you want to return an indeterminate number randomly, you can assign the K value of the entry parameter to K=random (M-N).
Random returns are not necessarily the number of different, the judgment can be removed, pay attention to i++ do not miss out. No longer given here.

#指定若干个字符/number, and then randomly return one (or more) characters/numbers, you can assigned the original word to an array, and then according to the subscript of the array
Determines the return value. There is no longer a function, you can try it yourself.

#另需指出, we use MCCOLOR.SETRBG (random (0xFFFFFF)) for randomly setting the color value of a MC, as described in the following example.
If you want to specify a gamut, you can use the function given above. If you don't know much about color objects, you can find help, and there's no discussion here.
The above functions are directly derived from the random, the following example, can be said to be derived functions of the derivative function, which will be used directly above the function given above, please note.

#6: Returns a specified length of a random uppercase English string


function RandomString (n) {
var Arraya = Randomkdiffer (1, +, 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, if written in lowercase, writes the return value to Arrayb.tolowercase (), or if it returns a case-insensitive string,
Remove the comment. If you want to return a string that does not specify a length, you can assign the entry parameter to random (n), which specifies only its upper bound. This function can also be used with the
Randomascii function implementation, left to everyone to think.

#7: Select random numbers in several regions

For example, select a random number between the two segments of the 1~20,45~70. Because the number of regions is undecided, it is inconvenient to write directly with a definite function,
The way we're going to do this is to use the switch statement to Orient, specifically we give a function that returns a number within a 1~20,45~70, and the rest of the region's readers should make their own changes.

function Randomarea () {
var a=random (2);
Switch (a) {
Case 0:
Return randomnm (1,20);
Case 1:
Return randomnm (45,70);
}
}
Note that we do not write the entry parameters, but directly in the function to determine the two-segment number, and the scope is also determined. In the case of three paragraphs, change to A=random (3);
Also add a case to it. Of course, you can also set the range of the number of paragraphs as the entry parameter, which is no longer an example. But doing so may increase the number of parameters,
I personally do not like a function that requires a lot of parameters. Similarly, we can also randomly return a number of letters or a few letters or letters plus a numeric field.
method is just a combination of the previous functions. Here, for example, returns a random letter of the specified uppercase segment.
As a reminder, the ASCII code A~Z for lowercase letters correspond to 97~122 respectively.

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;
}
}
It uses a function, Ord (char), which is an deprecated function. The ASCII code used to return a char character.

If you want to call a function anywhere, you need to change it a little bit, changing the function we wrote to a global function. So that you can easily call the system without marking the path
function. The method is as follows. For example: function Randomxiao If you want to declare a global function, you need to change the first line to:

_global.randomxiao=function (n,m) {
Statment
}

A friend who is not very clear about the concept of a global function is not intimidated by this noun.
This changes the function after the first line, in any place, such as in a MC, directly with (right, direct use, without adding _root path) Randomxiao (n,m) can be

C # stochastic function random () Typical usage highlights

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.