In this article, I'll show how to get a random subset of data from a
DataTable. This is useful when you already have queries that are filtered correctly, but it returns all the rows.
I came across this situation when I wanted to display a random tag cloud. I already had the query to get the keywords ordered by number of clicks and I wanted to created a tag cloud. Tags that are the most popular should have more chance to get picked and should be displayed larger than the less popular ones.
In this code snippet, there is everything you need:
Private Const MIN_FONT_SIZE As Integer = 9
Private Const MAX_FONT_SIZE As Integer = 14
Public Shared Function GetTags() As MediasTagsDataTable
Dim dt As MediasTagsDataTable = taMediasTags.GetDataValide
If dt Is Nothing OrElse dt.Rows.Count < 1 Then
Return New MediasTagsDataTable
Dim groupLenth As Integer = CType(Math.Floor_
(dt.Rows.Count / (MAX_FONT_SIZE - MIN_FONT_SIZE)), Integer)
Dim counter As Integer = 0
Dim groupCounter As Integer = 0
For Each row As MediasTagsRow In dt
row.c_FontSize = MIN_FONT_SIZE + groupCounter
counter += 1
If groupLenth <= counter Then
counter = 0
groupCounter += 1
Public Shared Function GetRandomSampleTags(ByVal KeyCount As Integer) _
Dim dt As MediasTagsDataTable = GetTags()
Dim rep As MediasTagsDataTable = New MediasTagsDataTable
Dim count As Integer = 0
Dim rand As New Random()
While count < KeyCount
Dim r As Integer = rand.Next(0, dt.Rows.Count - 1)
Dim tmpRow As MediasTagsRow = dt(r)
count += 1
This method is good because it doesn't require much work to get it working fast. It is a good concept when you are working with small tables, let's say less than 100 records.
If you have more than 100 records, an out of memory exception may occur since we are copying and duplicating rows. I would consider using a stored procedure instead.
Published: 4/19/2009 10:04 PM