-3

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();
        }
    }
}
Rob
  • 26,989
  • 16
  • 82
  • 98
Sarah
  • 17
  • 3
  • 2
    You have described at least three errors. **Give us a program that clearly demonstrates one of them and say which one**. Don't make the people who are trying to help you have to guess at what your problem is or what your code is. Make a small, **complete** example that **clearly** shows the problem. The error message has a location; tell us what the location is. – Eric Lippert Apr 22 '16 at 01:46
  • 2
    `for (int i; <= N; i++)` *what* is less than or equal to `N`? Also, what is `ChooseN`? It's not defined anywhere (and judging by your error, it's probably a string, not an int) – Rob Apr 22 '16 at 01:48
  • 1
    In the program above you use "ChooseN" without saying what it is. What is it? – Eric Lippert Apr 22 '16 at 01:48
  • And what line it is on. I can't follow your code, but to change a string to an int you need to parse it. There are multiple options. https://msdn.microsoft.com/en-us/library/bb397679(v=vs.110).aspx – kchinger Apr 22 '16 at 01:49
  • 1
    When you get past this issue, for a short tutorial on how to modify the distribution produced by the random number generator, see https://ericlippert.com/2012/02/21/generating-random-non-uniform-data/ – Eric Lippert Apr 22 '16 at 01:50
  • ChooseN is defined in a background program, it is a user defined field that is currently a list of numbers in a drop down box that the user selects, then that number is how many random numbers need to be generated. – Sarah Apr 22 '16 at 01:51
  • @Sarah I think you'll find that it's defined as `string ChooseN`, rather than `int ChooseN`. You'll need to decide which one is correct - and figure out how to store `N` if indeed it *should* be a string, and not an integer. – Rob Apr 22 '16 at 01:52
  • @Rob, it needs to be an integer in order to be an upper bound on i, but since its user defined, I don't know how to make sure it is a int – Sarah Apr 22 '16 at 01:56
  • Possible duplicate of [How do I generate a random int number in C#?](http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c) – Mostafiz Apr 22 '16 at 03:26

1 Answers1

-3

I figured it out myself. I was not reading in ChooseN correctly. This fixed it.

    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 };
        }
Sarah
  • 17
  • 3
  • oh, and apparently labeling anything pi will screw with C# – Sarah Apr 22 '16 at 20:38
  • Your code is quite odd and it will not compile. Also, your observation that `pi` _will screw with C#_ is not true. `pi` is not a reserved word. However, if you want to a use reserved word like `event` as an identifier you can prefix it with `@`. – Martin Liversage Apr 22 '16 at 21:08
  • It is perfectly legal to use `pi` as an identifier; for that matter, it is perfectly legal to use `π` as an identifier. And Martin is right, this code is erroneous. – Eric Lippert Apr 25 '16 at 13:49
  • Also, try to avoid abbrvs, and try to avoid "Hungarian prefix notation", like starting things with txt that are textual. Name things based on what they *mean*, using *full words* in camelCase. – Eric Lippert Apr 25 '16 at 13:52
  • My code compiled fine for me. The words i used arent abbreviations, they are mathematical terms in voting theory, that mean specific – Sarah Apr 26 '16 at 21:34
  • 2
    @Sarah: "num", "rnd", "pitxt", "txtN" "Gen" and "ClBtn" are not mathematical terms in voting theory. They are abbrevs because someone didn't want to type "number", "random" "piText", "textN", "Generate" and "CloseButton". **Name things based on what they mean, using full words**. – Eric Lippert May 03 '16 at 00:14