Home > database >  Array of random numbers in C# / .NET framework
Array of random numbers in C# / .NET framework

Time:02-28

I am trying to create a lottery program that would generate random numbers between 1 - 35. These numbers need to be unique as well, they can not be the same. With C#, running on the .NET framework.

I have been trying to get it to work, but I keep getting errors and I don't understand why. I have been googling and looking at videos - still I don't get what I am doing wrong.

This is my 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 help
{
    public partial class Form1 : Form
    {
        // The array 
        TextBox[] TextBoxArray;
    
        // the counter on how many times u pressed btn 
        private int counter = 1;

        public Form1()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;
            // The text boxes name 1 - 7 
            //TextBoxArray = new TextBox[] { Nr1, Nr2, Nr3, Nr4, Nr5, Nr6, Nr7 };
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            NrOfPull.Text = counter.ToString();
            counter  ;

            // Keep getting an error 
            // Error CS0443   Syntax error; value  
            // they are talking about the Parse(TextBoxArray[].Text); 
            int number = int.Parse(TextBoxArray[].Text);

            // Loop through 
            Random rand = new Random();

            for (int i = 0; i < number; i  )
            {
                // Generate a alotter by turn 
                int storedNumber;
                int[] randomLottoRow = new int[7];

                for (int row = 0; row < 7; row  )
                {
                    do
                    {
                        storedNumber = rand.Next(1, 35);
                    }
                    while (randomLottoRow.Contains(storedNumber));

                    randomLottoRow[row] = storedNumber;
                }}

            /*
             *  This will only let me generate numbers.. but wanna use Parse...  
           
            Random generator = new Random();
            for (int x = 0; x < 7; x  )
            {
                TextBoxArray[x].Text = generator.Next(1, 35).ToString();
                Console.WriteLine(TextBoxArray[x].Text = generator.Next(1, 35).ToString());

            }
            */

        }
    }
}

Would love to get feedback on what I am doing wrong Thank you so much :)

CodePudding user response:

This is one way of generating random sequences of numbers:

using System.Linq;

public static class Ex
{
    static readonly Random rng = new Random();

    /// <summary>
    /// Randoms the sequence.
    /// </summary>
    /// <param name="maxValue">The maximum number in drawing.</param>
    /// <param name="count">The number of numbers drawn.</param>
    public static int[] Lottery(int maxValue, int count)
    {
        return Enumerable.Range(1, maxValue 1)
            .OrderBy((x)=>rng.NextDouble())
            .Take(count).ToArray();
    }
}
static class Program
{
    static void Main(string[] args)
    {
        var seq = Ex.Lottery(35, 8);
        // draw 8 numbers ranging between 1-35
        Console.WriteLine(string.Join(",", seq));
        // 14,24,1,3,25,5,31,30
    }
}

CodePudding user response:

You can generate the numbers like this

var random = new Random();
var numbers = new HashSet<int>();
while (numbers.Count < 6)
{
    numbers.Add(random.Next(1, 36));
}
  • Related