Create Random list of Record

by Ruben 4/4/2008 5:10:00 PM

How to select record by create random selection using VB.Net.

ex: CreateRandomList(Percentage, TotalRecs, True)


******Module*******
Imports System
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Math
Imports System.Timers

Module modFunction
    Private Sub BubbleSort(ByVal theArray() As Integer)
        Dim pass As Integer, compare As Integer
        Dim hold As Integer

        For pass = 1 To (UBound(theArray) - 1)
            For compare = 1 To (UBound(theArray) - 1)
                If theArray(compare) > theArray(compare + 1) Then
                    hold = theArray(compare)
                    theArray(compare) = theArray(compare + 1)
                    theArray(compare + 1) = hold
                End If
            Next compare
        Next pass
    End Sub

    Public Function CreateRandomList(ByVal Percentage As Long, ByVal MaxDocs As Long, ByVal Whole As Boolean) As Long
        Dim iNumDocs As Integer, iIndex As Integer
        Dim Num As Integer, Used() As Boolean, Count As Integer
        Dim iTime As New Timers.Timer

        ' Variables for the computation when we want to get sampling via percentage
        Dim iItems As Integer, iIncrement As Integer, iItemsCtr As Integer
        Dim iUpperBound As Integer, iLowerBound As Integer

        Try
            If Not Whole Then
                iItemsCtr = 1
                iItems = MaxDocs * (Percentage / 100)
                iIncrement = Round(MaxDocs / iItems)

                ' Initialize bounds
                iLowerBound = 1
                iUpperBound = iIncrement
            End If

            ' Compute for the number of documents that should be extracted
            iNumDocs = Round(MaxDocs * (Percentage / 100))

            ReDim RandomList(0 To iNumDocs)
            iIndex = 1

            ReDim Used(0 To Val(MaxDocs))

            Randomize(iTime.Interval)

            For Count = 0 To Val(iNumDocs) - 1
                Do
                    If Whole Then
                        Num = (Rnd() * (Val(MaxDocs) - 1)) + 1
                    Else
                        ' Check for the last range
                        If iItemsCtr + 1 = MaxDocs Then
                            ' Means the last range have smaller value than the increment value
                            If iUpperBound + iIncrement > MaxDocs Then
                                iIncrement = Round((MaxDocs - iUpperBound) / 2)
                            End If
                        End If

                        ' Check if last range is less than the increment
                        If iItemsCtr = MaxDocs Then
                            If iUpperBound <> MaxDocs Then
                                iUpperBound = MaxDocs
                            End If
                        End If

                        If (iUpperBound - iLowerBound = 1) Then
                            If Rnd() > 0.5 Then
                                Num = Int((iUpperBound - iLowerBound + 1) * Rnd() + iLowerBound)
                            Else
                                Num = Int((iUpperBound - iLowerBound) * Rnd() + iLowerBound)
                            End If
                        Else
                            Num = Int((iUpperBound - iLowerBound + 1) * Rnd() + iLowerBound)
                        End If

                        iLowerBound = iUpperBound + 1
                        iUpperBound = iUpperBound + iIncrement

                        If iUpperBound > MaxDocs Then
                            iUpperBound = MaxDocs
                        End If

                        iItemsCtr = iItemsCtr + 1
                    End If
                Loop Until Not Used(Num)

                Used(Num) = True

                RandomList(iIndex) = Num
                iIndex = iIndex + 1
            Next Count

            BubbleSort(RandomList)
            CreateRandomList = iNumDocs

        Catch ex As Exception
            MessageBox.Show(ex.Message, "S2xDE", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Function
End Module

FREE PDF BOOK DOWNLOAD

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

9/7/2008 5:23:41 AM

Recieved Updates



Enter your email address:

Delivered by FeedBurner

About the author

Name of author RUBEN CORRAL
System Developer in outSourcing company for almost 8 years. I built this blogs just for fun, sharing idea's, contribute a piece of code, especially to newbie programmers.

E-mail me Send mail

Calendar

<<  September 2008  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal point of view. Sample source codes are free to modify or enhance for your own satisfaction.

Sign in

All brand names, logos and trademarks in this site are property of their respective owners.