hi i tried a code to implement non repeating random numbers from1 to 25..but its showing only 24 numbers..code is below..pls help me..or sujjest me a good code for non repeating random numbers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




namespace teasting
{
    class Program
    {
        static void Main(string[] args)
        {
            Random generator = new Random();


            int[] j = new int[26];
            for (int i = 0; i <= 25; i++)
            {
                j[i] = generator.Next(1, 26);
                //check:
                //for (int k = 1; k < i; k++)
                //{
                //    if (j[i] == j[k]&&j[i]>25)
                //    {

                //        j[i] = generator.Next(1, 26);
                //        goto check;
                //    }
                //}

                //Console.WriteLine(""+j[i].ToString());

            }
        check:
            for (int i = 0; i < 25; i++)
            {
                for (int k = i + 1; k < 25; k++)
                {
                    if (j[i] == j[k])
                    {
                        j[i] = generator.Next(1, 26);
                        goto check;

                    }


                }

            }
            for (int i = 0; i < 25; i++)
            {
                Console.WriteLine(j[i].ToString());
            }
            Console.ReadLine();
        }
    }
}

Edited 5 Years Ago by Manswab: n/a

I think

for (int i = 0; i < 25; i++)

I needs to be

(int i = 0; i <= 25; i++)

I think

for (int i = 0; i < 25; i++)

I needs to be

(int i = 0; i <= 25; i++)

its not working

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




namespace teasting
{
    class Program
    {
        static void Main(string[] args)
        {
            Random generator = new Random();


            int[] j = new int[26];
            for (int i = 0; i <= 25; i++)
            {
                j[i] = generator.Next(1, 26);
                //check:
                //for (int k = 1; k < i; k++)
                //{
                //    if (j[i] == j[k]&&j[i]>25)
                //    {

                //        j[i] = generator.Next(1, 26);
                //        goto check;
                //    }
                //}

                //Console.WriteLine(""+j[i].ToString());

            }
        check:
            for (int i = 0; i <= 25; i++)
            {
                for (int k = i + 1; k < 25; k++)
                {
                    if (j[i] == j[k])
                    {
                        j[i] = generator.Next(1, 26);
                        goto check;

                    }


                }

            }
            for (int i = 0; i <= 25; i++)
            {
                Console.WriteLine(j[i].ToString());
            }
            Console.ReadLine();
        }
    }
}

This works. you need to change

for (int i = 0; i <= 25; i++)

two times.

I have tested it

Yup It working But its giving 26 numbers .. i want to get exact 25 non repeating random numbers every tine whrn i run this...

This article has been dead for over six months. Start a new discussion instead.