一聚教程网:一个值得你收藏的教程网站

热门教程

ActionScript 3.0 Random用法

时间:2022-07-02 17:04:21 编辑:袖梨 来源:一聚教程网

一个简单的随机函数工具类,总共提供了9种静态方法来获取不同的随机值
随便写的,如果你还有什么更好的建议,请提出来,谢谢~

index.base.func.Random类:
代码:public final class Random
提供了9种静态方法来获取不同的随机值

构造函数:
public function Random()
如果直接实例化该类,将抛出错误

boolean静态属性:
public static function get boolean():Boolean
随机获取一个布尔值,即可能是true,也可能是false

wave静态属性:
public static function get wave():int
随机获取一个正负波动值,即可能是1,也可能是-1

integer静态方法:
public static function integer(num:Number):int
随机获取一个范围中的整数值

number静态方法:
public static function number(num:Number):Number
随机获取一个范围中的Number指

range静态方法:
public static function range(num1:Number,num2:Number,isInt:Boolean = true):Number
isInt:表示是否以整数随机
随机在一个指定范围获取数值

ranges静态方法:
public static function ranges(...args):Number
最后一个参数可以是逻辑值,用处等同于range的isInt参数,默认为true
随机在多个指定范围获取数值

string静态方法:
public static function string(str:String = "0-9,A-Z,a-z"):String
在指定范围随机一个字符

bit静态方法:
public static function bit(num:int,str:String = ""):String
在指定范围随机一个num位的字符串

color静态方法:
public static function color(red:String = "0-255",green:String = "0-255",blue:String = "0-255"):uint
在指定范围随机一个颜色值

举例:
复制内容到剪贴板代码:
import index.base.func.Random;

trace(Random.boolean+"(随机一个布尔值)");
trace(Random.color()+"(随机一个RGB颜色值)");
trace(Random.color("153-255","0-100","0-100")+"(随机一个偏红的RGB颜色值)");
trace(Random.wave+"(随机一个波动值)");
trace(Random.string()+"(随机一个字符)");
trace(Random.integer(4)+"(随机一个int值)");
trace(Random.number(4)+"(随机一个Number值)");
trace(Random.string("a-z,A-Z")+"(在一个范围随机字符)");
trace(Random.string("我,你,他")+"(在一个范围随机中文字符)");
trace(Random.ranges(5,4,3,-1,-10,-14,true)+"(在多个指定范围随机数值)");
trace(Random.range(4,9)+"(在一个指定范围随机数值)");
trace(Random.bit(8)+"(随机一个八位的字符串)");
trace(Random.bit(16,"0-1")+"(随机一个十六位的二进制)");
trace("…………")

//生成一个偏红的颜色色块
var sh:Shape = new Shape();
var rgb:uint = Random.color("204-255","0-153","0-153");
trace(rgb1.toString(16));
sh.graphics.beginFill(rgb);
sh.graphics.drawRect(100,100,100,100);
addChild(sh);

//生成一个颜色偏深或者偏淡的颜色色块
var sh1:Shape = new Shape();
var rgb1:uint = Random.color("0-20,240-255","0-20,240-255","0-20,240-255");
trace(rgb1.toString(16));
sh1.graphics.beginFill(rgb1);
sh1.graphics.drawRect(200,100,100,100);
addChild(sh1);

/**
* 输出:
* false(随机一个布尔值)
* 8370486(随机一个RGB颜色值)
* 12322387(随机一个偏红的RGB颜色值)
* -1(随机一个波动值)
* E(随机一个字符)
* 3(随机一个int值)
* 1.0007172282785177(随机一个Number值)
* m(在一个范围随机字符)
* 我(在一个范围随机中文字符)
* -14(在多个指定范围随机数值)
* 5(在一个指定范围随机数值)
* idPJav1l(随机一个八位的字符串)
* 1010100101010100(随机一个十六位的二进制)
* …………
* dd5506
* fef003
*/
类源代码:
复制内容到剪贴板代码:
package index.base.func{
       
        public final class Random{
               
                public function Random(){
                        throw new Error("随机函数工具包无法实例化!");
                }
               
                //获取一个随机的布尔值
                public static function get boolean():Boolean{
                        return Boolean(integer(2));
                }
               
                //获取一个正负波动值
                public static function get wave():int{
                        return integer(2) * 2 - 1;
                }
               
                //获取一个随机的范围整数值
                public static function integer(num:Number):int{
                        return Math.floor(number(num));
                }
               
                //获取一个随机的范围Number值
                public static function number(num:Number):Number{
                        return Math.random() * num;
                }
               
                //在一个范围内获取一个随机值,返回结果范围:num1 >= num > num2
                public static function range(num1:Number,num2:Number,isInt:Boolean = true):Number{
                        var num:Number = number(num2 - num1) + num1;
                        if(isInt) num = Math.floor(num);
                        return num;
                }
               
                //在多个范围获取随机值
                public static function ranges(...args):Number{
                        var isInt:Boolean = args[args.length - 1] is Boolean ? args.pop() : true;
                        var num:Number = randomRange(args);
                        if(!isInt) num += Math.random();
                        return num;
                }
               
                //获取一个随机字符,默认随机范围为数字+大小写字母,也可以指定范围,格式:a-z,A-H,5-9
                public static function string(str:String = "0-9,A-Z,a-z"):String{
                        return String.fromCharCode(randomRange(explain(str)));
                }
               
                //生成指定位数的随机字符串
                public static function bit(num:int,str:String = "0-9,A-Z,a-z"):String{
                        var reStr:String = "";
                        for(var i:int = 0; i < num; i ++) reStr += string(str);
                        return reStr;
                }
               
                //获取一个随机的颜色值
                public static function color(red:String = "0-255",green:String = "0-255",blue:String = "0-255"):uint{
                        return Number("0x" + transform(randomRange(explain(red,false))) +
                                                                 transform(randomRange(explain(green,false))) +
                                                                 transform(randomRange(explain(blue,false))));
                }
               
                //将10进制的RGB色转换为2位的16进制
                private static function transform(num:uint):String{
                        var reStr:String = num.toString(16);
                        if(reStr.length != 2) reStr = "0" + reStr;
                        return reStr;
                }
               
                //字符串解析
                private static function explain(str:String,isCodeAt:Boolean = true):Array{
                        var argAr:Array = new Array;
                        var tmpAr:Array = str.split(",");
                        for(var i:int = 0; i < tmpAr.length; i ++){
                                var ar:Array = tmpAr[i].split("-");
                                if(ar.length == 2){
                                        var arPush0:String = ar[0];
                                        var arPush1:String = ar[1];
                                        if(isCodeAt){
                                                arPush0 = arPush0.charCodeAt().toString();
                                                arPush1 = arPush1.charCodeAt().toString();
                                        }
                                        //此处如果不加1,将不会随机ar[1]所表示字符,因此需要加上1,随机范围才是对的
                                        argAr.push(Number(arPush0),Number(arPush1) + 1);
                                }else if(ar.length == 1){
                                        var arPush:String = ar[0];
                                        if(isCodeAt) arPush = arPush.charCodeAt().toString();
                                        //如果范围是1-2,那么整型随机必定是1,因此拿出第一个参数后,把范围定在参数+1,则就是让该参数参加随机
                                        argAr.push(Number(arPush),Number(arPush) + 1);
                                }
                                ar = null;
                        }
                        tmpAr = null;
                        return argAr;
                }
               
                //获取随机范围
                private static function randomRange(ar:Array):Number{
                        var tmpAr:Array = new Array;
                        var length:int = ar.length;
                        if(length % 2 != 0 || length == 0) throw new Error("参数错误!无法获取指定范围!");
                        //将所有可能出现的随机数存入数组,然后进行随机
                        for(var i:int = 0; i < length / 2; i ++){
                                var i1:int = ar[i * 2];
                                var i2:int = ar[i * 2 + 1];
                                if(i1 > i2){
                                        var tmp:Number = i1;
                                        i1 = i2;
                                        i2 = tmp;
                                }
                                for(i1; i1 < i2; i1 ++) tmpAr.push(i1);
                        }
                        var num:Number = tmpAr[integer(tmpAr.length)];
                        tmpAr = null;
                        ar = null;
                        return num;
                }
               
        }
       
}

热门栏目