Home

Random Non-Repeating Numbers

 

Introduction

In an assignment, you may need to use a series of random non-repeating numbers. This is an example of how you can generate such a series. The numbers are stored in an ArrayList object:

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

namespace WindowsApplication1
{
    class Numbers
    {
        public ArrayList RandomNumbers(int max)
        {
            // Create an ArrayList object that will hold the numbers
            ArrayList lstNumbers = new ArrayList();
            // The Random class will be used to generate numbers
            Random rndNumber = new Random();

            // Generate a random number between 1 and the Max
            int number = rndNumber.Next(1, max + 1);
            // Add this first random number to the list
            lstNumbers.Add(number);
            // Set a count of numbers to 0 to start
            int count = 0;

            do // Repeatedly...
            {
                // ... generate a random number between 1 and the Max
                number = rndNumber.Next(1, max + 1);

                // If the newly generated number in not yet in the list...
                if (!lstNumbers.Contains(number))
                {
                    // ... add it
                    lstNumbers.Add(number);
                }

                // Increase the count
                count++;
            } while (count <= 10 * max); // Do that again

            // Once the list is built, return it
            return lstNumbers;
        }
    }

    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnGenerate1_Click(object sender, EventArgs e)
        {
            Numbers nbs = new Numbers();
            const int Total = 25;
            ArrayList lstNumbers = nbs.RandomNumbers(Total);

            for (int i = 0; i < lstNumbers.Count; i++)
                lbxNumbers1.Items.Add(lstNumbers[i]);
        }

        private void btnGenerate2_Click(object sender, EventArgs e)
        {
            Numbers nbs = new Numbers();
            const int Total = 25;
            ArrayList lstNumbers = nbs.RandomNumbers(Total);

            for (int i = 0; i < lstNumbers.Count; i++)
                lbxNumbers2.Items.Add(lstNumbers[i]);
        }

        private void btnGenerate3_Click(object sender, EventArgs e)
        {
            Numbers nbs = new Numbers();
            const int Total = 25;
            ArrayList lstNumbers = nbs.RandomNumbers(Total);

            for (int i = 0; i < lstNumbers.Count; i++)
                lbxNumbers3.Items.Add(lstNumbers[i]);
        }

        private void btnGenerate4_Click(object sender, EventArgs e)
        {
            Numbers nbs = new Numbers();
            const int Total = 25;
            ArrayList lstNumbers = nbs.RandomNumbers(Total);

            for (int i = 0; i < lstNumbers.Count; i++)
                lbxNumbers4.Items.Add(lstNumbers[i]);
        }
    }
}

Here is an example of running the program:

Random Numbers

 

Home Copyright © 2006 FunctionX, Inc.