34

可能重复:
随机数生成器仅生成一个随机数

一个初学者的问题。我有一个非常简单的程序来画一条线,我想随机化位置,但是每次我创建一个新的 Random 实例时,它都会返回相同的值。哪里有问题?谢谢你。

private void Draw()
{
    Random random1 = new Random();
    int randomNumber1 = random1.Next(0, 300);
    Random random2 = new Random();
    int randomNumber2 = random2.Next(0, 300);
    Random random3 = new Random();
    int randomNumber3 = random3.Next(0, 300);
    Random random4 = new Random();
    int randomNumber4 = random4.Next(0, 300);
    System.Drawing.Graphics g = this.CreateGraphics();
    Pen green = new Pen(Color.Green, 5);
    g.DrawLine(green, new Point(randomNumber1, randomNumber2), 
                      new Point(randomNumber3, randomNumber4));
}

private void btndraw1_Click(object sender, EventArgs e)
{
    Draw();
}
4

7 回答 7

63

发生这种情况的原因是每次您执行新操作Random时都会使用时钟对其进行初始化。因此,在一个紧密的循环中(或一个接一个的多次调用),您会多次获得相同的值,因为所有这些随机变量都是用相同的种子初始化的。

解决这个问题:只创建一个随机变量,最好在你的函数之外并且只使用那个实例。

Random random1 = new Random();
private void Draw()
{
    int randomNumber1 = random1.Next(0, 300);
    int randomNumber2 = random1.Next(0, 300);
    int randomNumber3 = random1.Next(0, 300);
    int randomNumber4 = random1.Next(0, 300);
    System.Drawing.Graphics g = this.CreateGraphics();
    Pen green = new Pen(Color.Green, 5);
    g.DrawLine(green, new Point(randomNumber1, randomNumber2), new Point(randomNumber3, randomNumber4));
}
于 2013-02-03T15:10:25.930 回答
10

只需使用相同的实例:

Random random = new Random();
int randomNumber1 = random.Next(0, 300);
int randomNumber2 = random.Next(0, 300);
//...

编程中的随机数并不是真正随机的;它们基于一些独特的种子,这些种子被采用和操纵以生成看似一组随机数的东西。使用相同的种子将产生相同的数字集。

该类的默认构造函数Random使用自系统启动以来经过的毫秒数作为种子,因此实际发生的情况是使用了相同的种子。

真的没有理由创建不止一次的Random实例;单个实例将在每次执行代码时生成一组随机数字。

为了证明我上面对默认种子的陈述,我使用了反射:

// System.Random
/// <summary>Initializes a new instance of the <see cref="T:System.Random" /> class, using a time-dependent default seed value.</summary>
public Random() : this(Environment.TickCount)
{
}

Environment.TickCount

// System.Environment
/// <summary>Gets the number of milliseconds elapsed since the system started.</summary>
/// <returns>A 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started.</returns>
/// <filterpriority>1</filterpriority>
public static extern int TickCount
{
    [SecuritySafeCritical]
    [MethodImpl(MethodImplOptions.InternalCall)]
    get;
}
于 2013-02-03T15:09:40.643 回答
4

您不应该Random为每个数字创建一个新对象。相反,使用相同的对象:

Random r = new Random();

private void Draw()
{
    // Create 4 random numbers
    int[] numbers = Enumerable.Range(0, 4).Select(x => r.Next(0, 300)).ToArray();

    System.Drawing.Graphics g = this.CreateGraphics();
    Pen green = new Pen(Color.Green, 5);
    g.DrawLine(green, new Point(numbers[0], numbers[1]),
                      new Point(numbers[2], numbers[3]));
}
于 2013-02-03T15:16:26.303 回答
4

随机数生成器 (RNG) 实际上并不生成随机数。相反,它使用一种算法来定义一系列看似随机的数字。此序列取决于在seed您创建 RNG 时通过所述算法运行的序列。

默认情况下,RNG 是使用系统时钟作为种子创建的,因为每次程序运行时时钟通常都会发生变化,因此很难预测“随机”序列。

在您的情况下,时钟很可能在创建一个随机对象和另一个对象之间没有改变;可能是由于 CPU 内部对指令的重新排序。

正如 Blachshma 所说,最好只创建一个随机对象并仅使用它。

public static Random MyRNG = new Random(); // create a single static random object, that you can use across all classes
private void Draw()
{
    randomNumber1 = MyRNG.Next(0, 300);
    randomNumber2 = MyRNG.Next(0, 300);
    // and so forth
}

请记住,System.Random不保证任何实例都是线程安全的,这意味着如果您计划让多个线程共享同一个随机对象,则必须锁定它。

lock (MyRNG)
{
    randomNumber = MyRNG.Next(0, 300);
}

不这样做可能会破坏您的随机对象,从而导致后续调用仅返回 0。

于 2013-02-03T16:02:13.627 回答
3

您只需要一个 Random 类的实例。

private void Draw()
    {
        Random random1 = new Random();
        int randomNumber1 = random1.Next(0, 300);

        int randomNumber2 = random1.Next(0, 300);

        int randomNumber3 = random1.Next(0, 300);

        int randomNumber4 = random1.Next(0, 300);

        System.Drawing.Graphics g = this.CreateGraphics();
        Pen green = new Pen(Color.Green, 5);
        g.DrawLine(green, new Point(randomNumber1, randomNumber2), new Point(randomNumber3, randomNumber4));
    }


    private void btndraw1_Click(object sender, EventArgs e)
    {
        Draw();
    }
于 2013-02-03T15:10:56.170 回答
3
    private static readonly Random Random1 = new Random();

    private void Draw()
    {

        int randomNumber1 = Random1.Next(0, 300);
        int randomNumber2 = Random1.Next(0, 300);
        int randomNumber3 = Random1.Next(0, 300);
        int randomNumber4 = Random1.Next(0, 300);
        System.Drawing.Graphics g = this.CreateGraphics();
        Pen green = new Pen(Color.Green, 5);
        g.DrawLine(green, new Point(randomNumber1, randomNumber2), new Point(randomNumber3, randomNumber4));
    }


    private void btndraw1_Click(object sender, EventArgs e)
    {
        Draw();
    }
于 2013-02-03T15:11:36.910 回答
-4

.Net 需要的随机类是种子值,您可以使用日期值作为种子,它会起作用。

private void Draw()
    {
        Random random1 = new Random(unchecked((int)DateTime.Now.Ticks << (int)100));
        int randomNumber1 = random1.Next(0, 300);
        Random random2 = new Random(unchecked((int)DateTime.Now.Ticks << (int)200));
        int randomNumber2 = random2.Next(0, 300);
        Random random3 = new Random(unchecked((int)DateTime.Now.Ticks << (int)300));
        int randomNumber3 = random3.Next(0, 300);
        Random random4 = new Random(unchecked((int)DateTime.Now.Ticks << (int)400));
        int randomNumber4 = random4.Next(0, 300);
        System.Drawing.Graphics g = this.CreateGraphics();
        Pen green = new Pen(Color.Green, 5);
        g.DrawLine(green, new Point(randomNumber1, randomNumber2), new Point(randomNumber3, randomNumber4));
    }


private void btndraw1_Click(object sender, EventArgs e)
{
    Draw();
}
于 2013-02-03T15:19:22.740 回答