Solved

vb.net delete random from ArrayList

Posted on 2013-10-31
5
515 Views
Last Modified: 2013-10-31
Hello,
I have an ArrayList called "lastnames" with 20 items.
I would like to know whats the easiest way to remove random items from it leaving it with only 5 records.. how can I do this?
0
Comment
Question by:XK8ER
[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
5 Comments
 
LVL 2

Expert Comment

by:BakuRetsu_X
ID: 39615352
Create a random number generator object up to the size of your matrix.
Once you grab a number, take that as your array index to remove from the array.
Are you looking for code example?

Search vb.net random number generator on google.
Then search remove from array list vb.net.
0
 
LVL 1

Author Comment

by:XK8ER
ID: 39615359
yeah I tried that but for example I do

myAL.RemoveAt(5)
myAL.RemoveAt(8)
myAL.RemoveAt(12)

at this point i have only 17 items in the arraylist so when I do

myAL.RemoveAt(19)

it crashes
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39615682
Make sure the next random number you generate is never larger than the maximum index of your collection.

e.g.

Dim rand As New Random()
Dim myAL As New ArrayList() = SomeInitialization()
Dim toRemove As Integer = rand.Next(0, myAL.Count)

myAL.RemoveAt(toRemove)

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 39615786
Another quick example:
        Dim R As New Random
        While lastnames.Count > 5
            lastnames.RemoveAt(R.Next(lastnames.Count))
        End While

Open in new window

0
 
LVL 2

Expert Comment

by:BakuRetsu_X
ID: 39615795
Remember.  The array is constantly shrinking also. So each time you want the random number to be within the length of it.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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