Solved

vb.net delete random from ArrayList

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to identify a paragraph in the body of text? 2 49
Get sourcecode path 14 46
vb.net deleting excel sheet in workbook 11 29
vb.net sendkey 4 17
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:…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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