为什么随机数定义在循环里面每次输出的都是一样的呢

img


如果定义在循环的外面就不会,现在还不明白这个原理,一样有老哥解释一下🙈

我建议你做个简单的demo测试一下,比如:


```c#
public static void TestRandom()
        {           
            for (var i = 0; i < 10; i++)
            {
                Random random = new Random();

                var num = random.Next(0, 10);
                if (num < 10 * 0.5)
                {
                    Console.Write("0");
                    Console.Write(" ");
                }
                else
                {
                    Console.Write("1");
                    Console.Write(" ");
                }
            }
        }

```

仅供参考:
C:\Program Files (x86)\Microsoft SDK\src\crt\rand.c

/***
*rand.c - random number generator
*
*       Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

#ifndef _MT
static long holdrand = 1L;
#endif  /* _MT */

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
*       Seeds the random number generator with the int given.  Adapted from the
*       BASIC random number generator.
*
*Entry:
*       unsigned seed - seed to seed rand # generator with
*
*Exit:
*       None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
        unsigned int seed
        )
{
#ifdef _MT

        _getptd()->_holdrand = (unsigned long)seed;

#else  /* _MT */
        holdrand = (long)seed;
#endif  /* _MT */
}


/***
*int rand() - returns a random number
*
*Purpose:
*       returns a pseudo-random number 0 through 32767.
*
*Entry:
*       None.
*
*Exit:
*       Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
        void
        )
{
#ifdef _MT

        _ptiddata ptd = _getptd();

        return( ((ptd->_holdrand = ptd->_holdrand * 214013L
            + 2531011L) >> 16) & 0x7fff );

#else  /* _MT */
        return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
#endif  /* _MT */
}



Random random = new Random();放在for的外面(上面


```c#
using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
           Random random = new Random();
        for (var i = 0; i < 10; i++)
            {
               
                var num = random.Next(0, 10);
                if (num < 10 * 0.5)
                {
                    Console.Write("0");
                    Console.Write(" ");
                }
                else
                {
                    Console.Write("1");
                    Console.Write(" ");
                }
            }

      }
   }
}

```)

public class Random
public Random(int Seed);
// 摘要:
// Initializes a new instance of the System.Random class, using the specified seed
// value.
//
// 参数:
// Seed:
// A number used to calculate a starting value for the pseudo-random number sequence.
// If a negative number is specified, the absolute value of the number is used.

其实不用我说什么的