用于模拟短信群发情况的随机数产生

短信下发时的一些条件
每分钟最高发送量,每分钟最高发送量,每分钟标准发送量,发送总量,
最后在产生每分钟发送量的随机数时还加上了一个波幅值来控制随机数偏向标准量的波动,这样可以模拟出网路情况好和不好时的不同状态。

下面是随机数序列产生的类
public class dataclass
{
    Random rand 
= new Random();
    
//生成随机数;
    private int getrand(int min,int max)
    
{
        
int temprand = rand.Next(min, max);
        
return temprand;
    }

    
//生成带最大最小偏向和波幅的随机数
    private int getformatrand(int min, int max, int def,int wav)
    
{

        
int[,] temprand = new int[wav, 2];
        
        
for (int i = 0; i < wav; i++)
        
{
            temprand[i, 
0= getrand(min, max);
            temprand[i, 
1= (int)Math.Abs(temprand[i, 0- def);
        }

        
        
int temp = 0;
        
for (int j = 0; j < wav - 1; j++)
        
{
            
for (int i = 0; i < wav - 1; i++)
            
{
                temp 
= 0;
                
if (temprand[i, 1> temprand[i + 11])
                
{
                    temp 
= temprand[i, 1];
                    temprand[i, 
1= temprand[i + 11];
                    temprand[i 
+ 11= temp;
                    temp 
= temprand[i, 0];
                    temprand[i, 
0= temprand[i + 10];
                    temprand[i 
+ 10= temp;
                }

            }

        }

        
return temprand[00];
    }

    
/// <summary>
    
/// 生成发送数据
    
/// </summary>
    
/// <param name="min">单位时间内最小发送量</param>
    
/// <param name="max">单位时间内最大发送量</param>
    
/// <param name="def">单位时间内标准发送量</param>
    
/// <param name="wav">波幅,1波动最大,数字或大波动越小</param>
    
/// <param name="num">发送总量</param>
    
/// <returns></returns>

    public string[] getsendnum(int min, int max, int def, int wav, int num)
    
{
        
string temp = "";
        
        
for (int i = 0; i < num; )
        
{
            
int temprand = getformatrand(min, max, def, wav);

            
if ((i + temprand) >= num)
            
{
                temprand 
= num - i;
                i 
+= temprand;
                temp 
+= temprand;
            }

            
else
            
{
                i 
+= temprand;
                temp 
+= temprand + ",";
            }

            
        }

        
        
string[] str = temp.Split(new char[]{','});
        
return str;
        
    }

}

产生的是一组字符串数组,但都是数字,必要时可以转成整型应用就是了。

下面给两个演示图,主要是看波幅的控制
波幅为5时的情况
bf5.JPG
再来个波幅为50时的情况图
bf50.JPG
可以看到随机数产生的波动效果了吧。

其实思想很简单,在范围内产生的随机数的波动是控制不了的,那么怎么控制它偏向某个值呢?很简单,取多个随机数,从中找出离标准值最近的,也就是用它减标准值后,绝对值最小的那个,那么其它不要了,就取这个就行了,这样产生的一系列随机数就会向标准值偏向,每次产生的越多,最终得出的数值就离标准值越近,所以我这里这个波幅值(网络环境)其实就是定义的随机数每次产生的个数,在多少个中选一,当然数字越大,波幅越小了,哈哈!

这是一个小项目里用到的,接下来我要做的就是把生成的这组数据转成曲线图,对客户来说就看到一个比较完美的模拟了。
posted @ 2006-11-08 14:46  Aowind  阅读(1325)  评论(0编辑  收藏  举报