Introduction
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.
Analysis
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.
Implementation
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
End If
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
End If
Next
Return dt
End Function
Public Shared Function GetRandomSampleTags(ByVal KeyCount As Integer) _
As MediasTagsDataTable
Dim dt As MediasTagsDataTable = GetTags()
Dim rep As MediasTagsDataTable = New MediasTagsDataTable
Dim count As Integer = 0
Dim rand As New Random()
While count < KeyCount
Randomize()
Dim r As Integer = rand.Next(0, dt.Rows.Count - 1)
Dim tmpRow As MediasTagsRow = dt(r)
rep.ImportRow(tmpRow)
dt.Rows.RemoveAt(r)
count += 1
End While
Return rep
End Function
Pros
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.
Cons
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.
Category: CodeProject
Published: 4/19/2009 10:04 PM