Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 随机数发生器错误_C#_Random_Numbers_Generator_Simulator - Fatal编程技术网

C# 随机数发生器错误

C# 随机数发生器错误,c#,random,numbers,generator,simulator,C#,Random,Numbers,Generator,Simulator,我不熟悉C和随机数生成器,但需要为我正在学习的课程编写一个模拟器。我在使用for循环和用户定义的变量时遇到困难。我在Visual Studio中编码,需要用户从列表中选择一个数字或以文本形式输入该数字,但程序需要将其作为整数而不是字符串读取,然后使用该整数作为生成随机数的次数 稍后我需要给这个随机数生成器分配一个概率分布,但现在我只需要运行它!我得到了一个错误,它不能将int转换为string,或者将visa转换为string,这取决于我如何编码它。还有一个错误是我的局部变量i未赋值。我已经查看

我不熟悉C和随机数生成器,但需要为我正在学习的课程编写一个模拟器。我在使用for循环和用户定义的变量时遇到困难。我在Visual Studio中编码,需要用户从列表中选择一个数字或以文本形式输入该数字,但程序需要将其作为整数而不是字符串读取,然后使用该整数作为生成随机数的次数

稍后我需要给这个随机数生成器分配一个概率分布,但现在我只需要运行它!我得到了一个错误,它不能将int转换为string,或者将visa转换为string,这取决于我如何编码它。还有一个错误是我的局部变量i未赋值。我已经查看了类似生成器的其他代码,但在for循环中看不到有什么不同。请帮忙!下面是表单空间C代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GenerateProfile
{
    public partial class Form1 : Form
    {
        int N;
        public Form1()
        {
            InitializeComponent();
        }

        private void ChooseN_SelectedIndexChanged(object sender, EventArgs e)
        {

            N = ChooseN;
        }

        private void SBtn_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int num = rnd.Next(0, 100);
            pi.Text = num.ToString();
            for (int i; <= N; i++)
            {
                num = rnd.Next(0, 100);
                pi.Text = pi.Text + num.ToString();
            }
        }

        private void ClBtn_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

我自己想出来的。我没有正确地阅读ChooseN。这把它修好了

    private void Gen_Click(object sender, EventArgs e)
    {

        MessageBox.Show("N=", this.txtN.Text);
        N = Convert.ToInt32(txtN.Text);
        Random rnd = new Random();
        int num = rnd.Next(-1, 1);
        pitxt.Text = num.ToString();
        int[] = { num };
        for (int i = 1; i <= N; i++)
        {
            num = rnd.Next(-1, 1);
            pitxt.Text = pitxt.Text + "," + num.ToString();
            int[] = { int[], num };
        }

您至少描述了三个错误。给我们一个程序,清楚地演示其中的一个,并说明是哪一个。不要让那些试图帮助你的人猜测你的问题是什么或者你的代码是什么。制作一个小而完整的例子,清楚地说明问题所在。错误消息有一个位置;告诉我们位置是什么;在上面的程序中,您使用ChooseN时没有说明它是什么。它是什么?它在哪条线上。我无法理解您的代码,但要将字符串更改为int,需要对其进行解析。有多种选择。当你看完这个问题后,关于如何修改随机数生成器生成的分布的简短教程,请看哦,显然,用你的代码标记pi将要处理的任何东西是非常奇怪的,它不会编译。另外,你观察到的pi将与C结合是不正确的。pi不是一个保留字。但是,如果您想使用保留字(如事件)作为标识符,可以在其前面加@作为前缀;因此,使用π作为标识符是完全合法的。马丁是对的,这段代码是错误的。另外,尽量避免使用缩写,尽量避免使用匈牙利前缀符号,比如用文本文本开头。莎拉:num、rnd、pitxt、txtN Gen和ClBtn在投票理论中不是数学术语。它们是abbrev,因为有人不想键入number、random piText、textN、Generate和CloseButton。根据事物的意思命名,使用完整的单词。