现在的位置: 首页 > 综合 > 正文

为什RAND要给出种子

2013年12月01日 ⁄ 综合 ⁄ 共 2918字 ⁄ 字号 评论关闭

我认为原理是:
随机数种子是产生随机数的源头,然后通过一个复杂的函数(我不知,微软的),
将一个种子的值转化为随机数空间中的某一个点(数),好的函数会产生很大的空间,并且产生的随机数均匀的散布在空间中。(一个集合)

然后再随机选取一个  

如果种子相同,产生的集合也相同,
而然后,随机选取一个,如果超过了这个集合的总数,决定是有相同的,但相同的数不是重点,随机才是重点

 

可现实C#,

        Random randObj = new Random(); //生成的数字分布均匀;每个数字返回的可能性均相等。
        Console.WriteLine("{0} ", randObj.Next()); //随机选用一个
        Console.WriteLine("{0} ", randObj.Next());
        Console.WriteLine("{0} ", randObj.Next());

        Random randObj1 = new Random();
        Console.WriteLine("{0} ", randObj1.Next());
        Console.WriteLine("{0} ", randObj1.Next());
        Console.WriteLine("{0} ", randObj1.Next());

生成的数字分布均匀;每个数字返回的可能性均相等。

默认种子值是从系统时钟派生而来的。

Seed 用来计算伪随机数序列起始值的数字。

random

    // Methods
    public Random() : this(Environment.TickCount)
    {
    }

    public Random(int Seed)
    {
        this.SeedArray = new int[0x38];
        int num2 = 0x9a4ec86 - Math.Abs(Seed);
        this.SeedArray[0x37] = num2;
        int num3 = 1;
        for (int i = 1; i < 0x37; i++)
        {
            int index = (0x15 * i) % 0x37;
            this.SeedArray[index] = num3;
            num3 = num2 - num3;
            if (num3 < 0)
            {
                num3 += 0x7fffffff;
            }
            num2 = this.SeedArray[index];
        }
        for (int j = 1; j < 5; j++)
        {
            for (int k = 1; k < 0x38; k++)
            {
                this.SeedArray[k] -= this.SeedArray[1 + ((k + 30) % 0x37)];
                if (this.SeedArray[k] < 0)
                {
                    this.SeedArray[k] += 0x7fffffff;
                }
            }
        }
        this.inext = 0;
        this.inextp = 0x15;
        Seed = 1;
    }
初始化数组SeedArray

    private int InternalSample()
    {
        int inext = this.inext;
        int inextp = this.inextp;
        if (++inext >= 0x38)
        {
            inext = 1;
        }
        if (++inextp >= 0x38)
        {
            inextp = 1;
        }
        int num = this.SeedArray[inext] - this.SeedArray[inextp];
        if (num < 0)
        {
            num += 0x7fffffff;
        }
        this.SeedArray[inext] = num;
        this.inext = inext;
        this.inextp = inextp;
        return num;
    }

    public virtual int Next()
    {
        return this.InternalSample();
    }
然后再随机选取一个  

并没有随机,   
int num = this.SeedArray[inext] - this.SeedArray[inextp];

计算机是不可能产生真正的随机数的,只能靠计算,选取其中的数
所以种子在其中占有很重要的,

如果没有参数,则使用与时间相关的默认种子值。

        this.inext = 0;当前一个
        this.inextp = 0x15;下一个随机数
====================
sql中是
SELECT RAND(1)
SELECT RAND(1)
SELECT RAND(1)

0.71359199321292355
0.71359199321292355
0.71359199321292355

结果是相同

SELECT RAND()
SELECT RAND()
SELECT RAND()
                                                     

0.34344339282376501
0.21090395019612362     
0.38893187174817562
每次不相同
 ====================

vb
Randomize   语句               
    
  初始化随机数生成器。  
   
  语法  
   
  Randomize   [number]  
   
  可选的   number   参数是   Variant   或任何有效的数值表达式。  
   
  说明  
   
  Randomize   用   number   将   Rnd   函数的随机数生成器初始化,该随机数生成器给   number   一个新的种子值。如果省略   number,则用系统计时器返回的值作为新的种子值。  
   
  如果没有使用   Randomize,则(无参数的)Rnd   函数使用第一次调用   Rnd   函数的种子值。  
   
  注意   若想得到重复的随机数序列,在使用具有数值参数的   Randomize   之前直接调用具有负参数值的   Rnd。使用具有同样   number   值的   Randomize   是不会得到重复的随机数序列的。

虽然语言不同,Rand有些使用上不同,但种子,产生集合,思维上还是大约相同的
  

【上篇】
【下篇】

抱歉!评论已关闭.