Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I know how to create a random numbers in C#.

But I want to generate 10 digit random number (i.e.) Each digits are unique.

1234567890
1523678902

Similarly I want unrepeated random numbers with unrepeatable digits.

How to do this in C#.
Posted
Comments
Suvabrata Roy 24-Jul-14 0:34am    
Each digits are unique

Mean should not repeated where they are previously or always a new number
Sergey Alexandrovich Kryukov 24-Jul-14 1:09am    
You problem is not exactly specified. What are the constraint for numbers and their distribution? For example, do they have to be 10 digits? Is 0123456789 a valid output (it is equivalent to 123456789)? And so on...
—SA
KUMAR619 24-Jul-14 2:52am    
Sir, 0123456789 is a valid input cause I'm gonna split each and every character for further process.
How to do this sir
Sergey Alexandrovich Kryukov 24-Jul-14 12:47pm    
The rest of the spec is covered by "and so on"... :-)
—SA
Bernhard Hiller 24-Jul-14 2:17am    
Homework!

C#
ArrayList draw = new ArrayList();
Random cards = new Random();

public int RandomCards()
       {
           bool mark = false;
           int val = 0;

           while (mark == false)
           {
               val = RandomC();
               bool flags = CardCheck(val);
               if (flags == true)
               {
                   mark = true;
                   draw.Add(val);
               }
           }
           return val;
       }

public int RandomC()
        {
           // Returns a random card
           return cards.Next(1, 52);
        }

 private bool CardCheck(int random)
        {
            // creates a for loop to itterate through the array
            for (int i = 0; i < draw.Count; i++)
            {
                int calc = (int)draw[i];
                if (random == calc)
                {
                    return false;
                }
            }
            return true;
        }


This is code from a blackjack applicatiion I did a while ago.
This part is the random I used to draw new cards.
RandomCards runs through selecting a random card(between 1 and 52), after selecting the random card it checks an arraylist draw to see if the card is in the arraylist already, if it is in the arraylist it then selects a new random card and checks it again, until the card does is not in the arraylist, then it adds it to the arraylist ensuring that it cannot be selected again.

Now you can use this method of ensuring that each digit in the number is unique. also using the same method on the number should work, if you have it nested(i.e) you request the number to be generated, then while generating the number you have each digit of the number checked and added, then after you have the required length of digits you can check and add the number as to a different arraylist.

I hope this helps you get on the right track to solve the problem.
 
Share this answer
 
Comments
KUMAR619 24-Jul-14 2:59am    
Thanks for helping.
But My task was to generate virtual keyboard.
If all the 10 digits are different I can split the numbers into 10 characters.
So that I can assign value to each button in virtual keyboard.

So if I create a distinct digit random number I can achieve this.
Thanks once again Sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900