Solved

vb.net delete random from ArrayList

Posted on 2013-10-31
5
509 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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now