Solved

vb.net delete random from ArrayList

Posted on 2013-10-31
5
513 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
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 85

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

790 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