We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,991 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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();
        }
    }
}
2
Contributors
5
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
6
Views
Manswab
Newbie Poster
13 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I think

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

I needs to be

(int i = 0; i <= 25; i++)
superjj
Light Poster
43 posts since Nov 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

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
Skill Endorsements: 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
43 posts since Nov 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

I have tested it

superjj
Light Poster
43 posts since Nov 2010
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

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
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0710 seconds using 2.69MB