Random.next () returns nonnegative random numbers;
Random.next (INT) returns a nonnegative random number that is less than the specified maximum value
Random.next (int,int) returns a random number within a specified range, for example ( -100,0) Returning negative numbers
1, random (number) function introduction
See the Help document, simply to mention, random (number) returns a random 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 between 0~1 with 14-bit precision, noting that there are no parameters.
Example:
Trace (Math.random ());
3, the Custom function
The random numbers we sometimes need are not that simple.
For example, we want to return a random number with two decimal digits, return a random number between two digits, return a random number of letters, return multiple random numbers, and so on,
All of these require that we write our own functions to implement them. The following code is copied directly to the first frame of the main scene and can be invoked. Note that some functions require entry parameters.
#1: Returns a total of n digits, where m is a random number of decimal digits
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
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
function Randomascii () {
var c = string.fromcharcode (random (+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
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 element in the array Arrayk is the resulting value. Notice that we borrowed the random (m-n+1) +n to return a random number of n~m. So M itself will be returned.
If you want to return the number within m, you can change the n value to 0. If the number of uncertainties is to be returned randomly, the K value of the inlet parameter can be assigned to K=random (M-N);
Random return is not necessarily the number of different, the judge to remove it, pay attention to i++ do not miss out. This is no longer given.
#指定若干个字符/number, and then randomly return one (or more) characters/numbers, you can assignments the original word to an array, and then according to the array of subscript to
Determines the return value. No more functions here, you can try it on your own.
#另需指出, we use MCCOLOR.SETRBG (random (0xFFFFFF) for randomly setting the color value of a MC, which is illustrated in the following example.
If you want to specify a color field, you can use the function given above. If you don't know much about a color object, you can look for help, which is not discussed here.
The above functions are directly derived from the random, the following example, can be said to be derived function derivative functions, which will be used directly to the function given above, please note.
#6: Returns a random uppercase English string of a specified length
function RandomString (n) {
var Arraya = randomkdiffer (1, NUM, 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 as Arrayb.tolowercase () if it is to be written in lowercase, and if a case-insensitive string is returned,
The annotation is removed. If you want to return a string with an unspecified length, you can assign the entry parameter to random (n); This function can also be used
Randomascii function implementation, leaving everyone to think for themselves.
#7: Selecting random numbers in several areas
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 certain function,
The method we use is to use the switch statement to direct, we give a function, return the number of a 1~20,45~70, other areas of the reader please change.
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 we determine the two paragraphs directly in the function, and the scope is OK. If it is three paragraphs, change to A=random (3);
Also add a case to it. Of course, you can also set the range of paragraph number as the entry parameter, here is no longer an example. But doing so may increase the number of parameters,
Personally, I don't like a function that takes a lot of parameters. Similarly, we can also randomly return a number of letters or letters or letters plus numbers.
Method is also just a combination of the first few functions. Here, for example, returns a random letter of the specified uppercase paragraph.
Note that the ASCII code a~z of the lowercase letters correspond to the 97~122 respectively.
function Randomaarea (a,b) {
if (ord (a) <= ord (b) && 65<=ord (a) && ord (b) <=) {
retur N String.fromCharCode (RANDOMNM (Ord (a), Ord (b)));
else {return
false;
}
}
It uses a function ord (char), which is an deprecated function. The ASCII code that returns the char character.
If you want to call a function anywhere, you need to change it a little bit, changing the function we write to a global function. So 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 as a global function, you need to change the first line to:
_global.randomxiao=function (n,m) {
//statment
}
The concept of global functions not very clear friends do not have to be intimidated by this noun.
This changed the function after the first line, in any place, such as in a MC, direct use (right, direct use, without adding _root path) Randomxiao (n,m) can be.
Above this C # learning notes-random function random () The use of detailed information is small to share all the content of everyone, I hope to give you a reference, but also hope that we support the cloud habitat community.