Solved

Remove duplicates in random number generator

Posted on 2010-11-23
15
1,350 Views
Last Modified: 2012-05-10
I am confused on how to use LINQ and remove duplicate numbers from  my random number generator below is the code that I have written
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cunningham_CSC153_A13_P2
{
    class Program
    {
        static void Main(string[] args)
        {
            Random randomNumber = new Random();

            int[] num;
            num = new int[20];

            List<int> ranNum = new List<int>();

            for(int count = 0; count <= 29; count++)
            {
                //List<int> ranNum = new List<int>();
                ranNum.Add(randomNumber.Next(1,20));
                

               
               
            }
            var sortRanNum =
                 from value in ranNum
                 orderby value
                 select value;

            Display(sortRanNum, "Random Numbers sorted in ascending order");

            Console.WriteLine();

            var sortDecRanNum =
                 from value in ranNum
                 orderby value descending
                 select value;

            Display(sortDecRanNum, "Random Numbers sorted in descending order");

            Console.WriteLine();

            var duplicatesRemoved =
                 from value in ranNum
                 orderby value 
                 select value;

            Display(duplicatesRemoved, "Random Numbers and duplicates removed");



           
               


        }
        public static void Display<T>(IEnumerable<T> results, string header)
        {
            Console.WriteLine("{0}: ", header);
            foreach (T element in results)
                Console.WriteLine(element.ToString());

            Console.WriteLine();
        }
    }
}

Open in new window

0
Comment
Question by:rcham68
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 34198772
ranNum = ranNum.Distinct();
0
 

Author Comment

by:rcham68
ID: 34198868
Exactly where does this go?  Here is where I put it but it does not work

 var duplicatesRemoved =
                 from value in ranNum
                 orderby value
                 select ranNum = ranNum.Distinct();
           
            Display(duplicatesRemoved, "Random Numbers and duplicates removed");
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34198890
I would put it right after the for loop.

 for(int count = 0; count <= 29; count++)
            {
                //List<int> ranNum = new List<int>();
                ranNum.Add(randomNumber.Next(1,20));
               
            }
ranNum = ranNum.Distinct();
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rcham68
ID: 34198946
Can not put it there it gives me an error explicit conversion exist
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34198982
just cast the distinct to (List<int>)
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34199007
sorry, that doesn't work....
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34199031
this worked

(from num in ranNum
                 select num).Distinct().ToList();
0
 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
ID: 34199034
ranNum = (from num in ranNum
                 select num).Distinct().ToList();
0
 

Author Comment

by:rcham68
ID: 34199093
Thanks
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34199102
np, sorry about the false ones -- wasn't able to test right away
0
 

Author Comment

by:rcham68
ID: 34199143
ranNum = (from num in ranNum
                 select num).Distinct().ToList();

Where did you put this? Every where I put it I am getting a conflict with previous declaration of num
0
 

Author Comment

by:rcham68
ID: 34199150
ranNum = (from num in ranNum
                 select num).Distinct().ToList();

Where did you put this? Every where I put it I am getting a conflict with previous declaration of num
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34199163
num was something that was unique to my testing program -- change the name

ranNum = (from n in ranNum
                 select n).Distinct().ToList();
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34199168
after the for
0
 

Author Comment

by:rcham68
ID: 34199229
Got it working thanks a lot.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question