() Returns a non-negative random number;
(Int) Returns a non-negative random number smaller than the specified maximum value
(Int,Int) Returns a random number in the specified range, for example (-100, 0) returns a negative number
1. Introduction to random(number) function
See the help documentation, let me mention it briefly. random(number) returns a random integer between 0~number-1. The parameter number represents an integer.
Example:
trace(random(5));
2、()
See the help documentation. Return a number between 0 and 1 with 14-bit accuracy, and note that there are no parameters.
Example:
trace(());
3. Custom functions
Sometimes the random numbers we need are not that simple.
For example, we want to return a random number with two decimal places, return a random number between two numbers, return a letter random number, return multiple random numbers, etc.
All of these require us to write functions ourselves to implement them. The following code can be called by copying it directly to the first frame of the main scene. Note that some functions require entry parameters.
#1: Return a random number with a total of n digits, where m digits are decimals
function randomXiao(n,m){ var a = (10, n+m); var b = random(a); return b=b/(10, m); }
You can use trace(randomXiao(3,2)); to experiment. This function is simple. (n,m) is used to return a number with n as the base and m as the exponent. power!
#2: Return 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 that the range of random numbers is m-n, and adding 1 makes m also in it. Add n to ensure that the random number is n as 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 think more general is to use -random(n);
#3: Return a letter
function randomAscii(){ var c = (random(26)+65); if(random(2)){ return (); } return c; }
#4: Return a random letter that is not case sensitive
If you want to return to capitalization, just remove the if conditional sentence. If you want to return to lowercase, you can change the conditional sentence to constant validity, or remove the condition, and change the last sentence to:
return (); (number) function returns the ASCII code of number representing the number.
toLowerCase() is used to convert uppercase letters to lowercase.
#5: Return a k mutually different 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 =(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 obtained value. Notice that we borrowed random(m-n+1)+n to return a random number of n~m. So m itself will be returned.
If you want to return a number within m, you can change the value n to 0. If you want to randomly return an uncertain number, you can assign the K value of the entry parameter to k=random(m-n);
Randomly return numbers that are not necessarily different, just remove the judgment. Be careful not to miss i++. Not given here.
#Specify several characters/digits, and then randomly return one (or more) characters/digits. You can assign the original characters to an array and then use the subscript of the array.
Determines the return value. No more functions are mentioned here, you can try it yourself.
#It should also be pointed out that for randomly setting the color value of a MC, we mostly use (random(0xFFFFFF)); the following example will be explained.
If you want to specify a color gamut, you can use the function given above. If you don't know much about Color objects, you can check it for help, so I won't discuss it here.
The above functions are directly derived from random. Here is another example. It can be said to be a derivative function of a derivative function, where the functions given above will be directly used. Please note.
#6: Return a random capital English string of specified length
function randomString(n){ var arrayA = randomKdiffer(1, 26, n); var arrayB = ""; for (var i = 0; i < n; i++) { c=(arrayA[i]+64); /* if(random(2)){ c=(); } */ arrayB = arrayB+c; } return arrayB; }
Note that the StringCharCode method, if it wants to be written in lower case, write the return value as (); if it returns a case-insensitive string,
Then remove the comment. If you want to return a string with no specified length, you can assign the entry parameter to random(n); so that only its upper limit is specified. This function can also be used
The randomAscii function implementation is left to everyone to think about it for themselves.
#7: Select random numbers in several areas
For example, choose a random number between the two segments: 1~20, 45~70. Because the number of regions is not determined, it is very inconvenient to write directly with a definite function.
The method we want to use is to use the switch statement for direction. For specific purposes, we give a function, returning a number within 1~20, 45~70. Readers in other areas please change it by themselves.
function randomArea(){ var a=random(2); switch(a){ case 0: return randomNm(1,20);break; case 1: return randomNm(45,70);break; } }
Note that we did not write the entry parameters, but directly determine that it is a two-part number in the function, and the range is also determined. If it is three segments, change to a=random(3);
Just add a case. Of course, you can also set the range of the number of paragraphs as the entry parameter, so I won’t give any more examples here. But doing so may increase the parameters.
I personally don't like a function that requires a lot of parameters. Similarly, we can also randomly return an alphabetical field or a number with several alphabetical fields or a number field.
The method is just a combination of the first few functions. Here is just one example, returning a random letter of the specified capital letter segment.
As a reminder, the ASCII codes a~z of lowercase letters correspond to 97~122 respectively.
function randomAArea(a,b){ if (ord(a) <= ord(b) && 65<=ord(a) && ord(b) <= 90) { return (randomNm(ord(a), ord(b))); } else { return false; } }
A function ord(char), which is a deprecated function. It is used to return the ASCII code of the char character.
If you want to call functions anywhere, you need to change it a little and change the function we write to a global function. This way you can freely call the system without specifying the path
The function is the same. The method is as follows. For example: If the function randomXiao is to be declared as a global function, you need to change the first line to:
_global.randomXiao=function(n,m){ //statment }
Friends who don’t know much about the concept of global functions should not be scared by this noun.
In this way, after changing the first line of the function, use it directly anywhere, such as in an MC (right, use it directly, no need to add the _root path) randomXiao(n,m).
The above C# study notes - The detailed explanation of the usage of the random function Random() is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.