jeudi 21 avril 2016

C# Random Number Generator Error

I am new to C# and random number generators, but need to code a simulator for a course I am taking. I am having difficulty with my for loop and my user-defined variables. I am coding in Visual Studio and need the user to select a number from a list (or input the number as text), but for the program to read it as an integer, not a string, and then use this integer as the number of times to generate a random number.

I will need to assign a probability distribution to this random number generator later, but right now I just need the thing to run! I am getting an error that it cannot covert int to string (or visa versa depending on how I code it). As well as getting an error that my local variable i is unassigned. I have looked at others codes for similar generators and I cannot see a difference in my for loop. Please help! Below is the form space C# code:

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();
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire