Question how to generate Unique numbers?

buta

New member
Joined
Nov 2, 2020
Messages
2
Programming Experience
Beginner
C#:
using System;

namespace G06_20201027
{
    class Program
    {
        static void Main()
        {
            Random r = new Random();
            int[] x = new int[10];

            for (int i = 0; i < x.Length; i++)
            {
                x = r.Next(15);
            }

            for (int i = 0; i < x.Length; i++)
            {
                Console.WriteLine(x);
            }

            Console.ReadKey();
        }
    }
}
 
Last edited by a moderator:
Creating a thread with a title and some code but nothing else is not acceptable. The very first thing you should be doing is providing a full and clear explanation, which includes the code. Once you're done, then you can add a title that summarises the topic of the thread with an appropriate prefix. As it stands, I have no idea whether you're trying to tell us how something should be done or asking us. If you're asking us, I have no idea what the actual problem is because you've made no attempt to explain. If you would like us to volunteer our time to help you then you need to take the time to help us do so.
 
Creating a thread with a title and some code but nothing else is not acceptable. The very first thing you should be doing is providing a full and clear explanation, which includes the code. Once you're done, then you can add a title that summarises the topic of the thread with an appropriate prefix. As it stands, I have no idea whether you're trying to tell us how something should be done or asking us. If you're asking us, I have no idea what the actual problem is because you've made no attempt to explain. If you would like us to volunteer our time to help you then you need to take the time to help us do so.
thanks and sorry for bad thread, i will do in right way next time
 
Or do it this time. You probably can't edit your existing post until you've been here a bit longer but you can certainly add a new post that provides the explanation that you ought to have provided in the first place. There are people here who would like to help but we need to know what we're helping with.
 
I'm not sure whether this is what you were actually trying to ask about without explaining it but this bit doesn't make sense:
C#:
x = r.Next(15);
You have declared x as an array and then you are trying to assign an int value to it. Think about what you actually need to do there. If x is an array then you need to put that int value into that array. It's not hard to find information on how to do that.
 
Also, there is a difference between unique numbers vs. random numbers. You could have simply filled an array with their corresponding index numbers and the numbers would be unique.
 
Back
Top Bottom