C#随机数生成器每次仍给出相同的数字 [英] C# random number generator still gives same number each time

查看:285
本文介绍了C#随机数生成器每次仍给出相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//创建一个由十行组成的字符串数组. 字符串[] personalNumbers;//将personalNumbers声明为10个元素的数组 personalNumbers =新字符串[10];//= {第一个号码",第二个号码",第三行"等}

// Create a string array that consists of ten lines. string[] personalNumbers; // declare personalNumbers as a 10-element array personalNumbers = new string[10]; //= { "First number", "Second number", "Third line", etc}

        for (int i = 0; i < 9; i++)   // populate the array  with 10 random values
        {
            Random random = new Random();
            int randomNumber = random.Next(1, 50);

            string RandomNumberText = Convert.ToString(randomNumber);

            personalNumbers[i] = RandomNumberText;   
        }

您好,我知道此SEEMS是以前询问的问题的副本,但我正在尝试 生成一系列介于1到50之间的随机数以填充数组

Hi, I know that this SEEMS to be a duplicate of previously asked questions, but I am trying to generate a series of random numbers between 1 and 50 to populate an array

问题是,如果我按照上课的要求去做,每个数字都是相同的

The problem is, if I do it as we were taught in class, each number is the same

我知道问题是紧密循环导致随机数以相同的数字播种

I know that the problem is that the tight loop causes the random to be seeded with the same number

然而,其他任何线程都没有解决的问题是如何在使用循环迭代时解决此问题....

What NONE of the other threads addresses however, is HOW to fix this problem when using a loop iteration....

到目前为止,所有的解释都超出了我们的水平,以至于我(和其他问者BTW)都不知道如何实现它们作为解决方案,我也无法在课堂上提交这些信息,因为它们是我们拥有的技术没有覆盖

All of the explanations thus far are so far advanced beyond our level that I (and the other askers BTW) have no clue how to implement them as a solution, which I also cannot submit in class as they are techniques that we have not covered

Microsoft的教程坚持认为,将随机放入循环是正确的解决方案

The tutorials at Microsoft insist that putting the random inside the loop is the right solution

我尝试将random的实例放在循环外,然后从循环内调用它,但这引起了异常

I've tried putting an instance of random outside the loop and then calling it from inside the loop but this has caused an exception

有没有一种简单的方法可以使用随机数来创建一系列不会出现此问题的随机数?

Is there a straightforward way to use random to create a series of random numbers that doesn't run into this problem?

推荐答案

我尝试将随机实例放到循环外,然后 从循环内部调用它,但这导致了异常

I've tried putting an instance of random outside the loop and then calling it from inside the loop but this has caused an exception

这里有两个具体示例,一个用于控制台应用程序,另一个用于WinForms应用程序.

Here are two concrete examples, one for a Console app, and the other for a WinForms app.

这是在控制台应用程序中声明它的一种方法.可以在应用程序中的任何地方使用random,甚至可以使用Main()以外的方法:

This is one way to declare it in a Console app. random can be used from anywhere within the application, even in methods other than Main():

class Program
{

    private static Random random = new Random();

    static void Main(string[] args)
    {
        // ... code ...

        for (int i = 0; i < 9; i++)   // populate the array  with 10 random values
        {
            int randomNumber = random.Next(1, 50);

            personalNumbers[i] = randomNumber.ToString();
        }

        // ... code ...
    }

}

这是声明要在WinForms应用中使用的一种方法.在此示例中,random可以在Form1中的任何位置使用:

This is one way to declare it for use in a WinForms app. random in this example can be used anywhere within Form1:

public partial class Form1 : Form
{

    private Random random = new Random();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // ... code ...

        for (int i = 0; i < 9; i++)   // populate the array  with 10 random values
        {
            int randomNumber = random.Next(1, 50);

            personalNumbers[i] = randomNumber.ToString();
        }

        // ... code ...
    }

}

这应该涵盖大多数简单的家庭作业.这里没什么好看的.

This should cover most simple homework assignments. Nothing fancy here.

这篇关于C#随机数生成器每次仍给出相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆