954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I cant implemet non repeating random numbers

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();
        }
    }
}
Manswab
Newbie Poster
13 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

I think

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

I needs to be

(int i = 0; i <= 25; i++)
superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

I think

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

I needs to be

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


its not working

Manswab
Newbie Poster
13 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
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.

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

I have tested it

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 
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...

Manswab
Newbie Poster
13 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: