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

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());
}
}
}
}``````
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
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
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
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());
}
}
}
}``````

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

I have tested it

superjj
Light Poster
43 posts since Nov 2010
Reputation Points: 10
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