CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2010
    Posts
    3

    Lightbulb Assigning a random number to an array List c#?

    please help, im trying to assign a random number variable which ive created called 'custID' in a Customer Class to an array list of customers called 'customerList' which is in the program.

    How can I do this?
    Any help is appreciated.

    Thanks

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Assigning a random number to an array List c#?

    First off, if you are trying to use random numbers to generate unique id's.. don't. Use a GUID.

    Anyway, look at the Random class.

  3. #3
    Join Date
    Sep 2010
    Posts
    7

    Re: Assigning a random number to an array List c#?

    Code:
      Random rnd = new Random();
                System.Collections.ArrayList rndID = new System.Collections.ArrayList();
                int numsID = 20;
                int minID = 1;
                int maxID = 100;
                for (int c = 1; c <= numsID; c++)
                {
                    rndID.Add(rnd.Next(minID, maxID));
                }

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: Assigning a random number to an array List c#?

    Quote Originally Posted by ClassCoder View Post
    Code:
      Random rnd = new Random();
                System.Collections.ArrayList rndID = new System.Collections.ArrayList();
                int numsID = 20;
                int minID = 1;
                int maxID = 100;
                for (int c = 1; c <= numsID; c++)
                {
                    rndID.Add(rnd.Next(minID, maxID));
                }
    I don't think you should be encouraging people to use ArrayList as it is deprecated. Generic List<> is strongly typed, has increased functionality and greater performance and memory usage. It's available on all versions of .NET since 2.0 I believe.

  5. #5
    Join Date
    Sep 2010
    Posts
    7

    Re: Assigning a random number to an array List c#?

    Quote Originally Posted by Chris_F View Post
    I don't think you should be encouraging people to use ArrayList as it is deprecated. Generic List<> is strongly typed, has increased functionality and greater performance and memory usage. It's available on all versions of .NET since 2.0 I believe.

    i agree with you .. i don't never use ArrayList in my applications .. i prefer using List<>,HashTable<,>,Dictionary<,>


    when we use LINQ IEnumrable<>

  6. #6
    Join Date
    Nov 2010
    Posts
    3

    Re: Assigning a random number to an array List c#?

    thanks for the feedback - i've been told to use array list so i'm guessing i have to.

    I have an arraylist called customerList and all i want to do is give each of the customers a Random number

    so that when i execute the command to display all the customers in that array, it displays its ID alongside each individual customer.

    Ive researched GUID's and they would have been ideal but they are too long and Random numbers are sure to be fine as its just a mini project with a few customers in the array.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured