tutlogger

Randomization Examples in VB.Net




Have you experienced playing the fruit machine lottery? I am asking this question, because you might be wondering how these random combination of  fruit images is generated. These machines you see aren't controlled by human manually. They are designed to do randomization process, to generate a random permutation of a sequence (such as the numbers used in a lottery game). So, the result it generates are fair and just.

Can a computer also do a randomization process? Yes, absolutely! Computers are invented to perform billions of calculations each second and do mathematical arithmetic very quickly. It is a multitasking machine that is able to do more type of task that a person cannot possibly do in a very short span of time.


In this tutorial, I will show you how to create an application in your computer that can do different randomization techniques using Visual Studio 2012. The project will include simple number randomization, randomizing letters in a word, generating random codes, selecting items in a list using randomization and random selection of images.


I will provide the codes directly. You can download the working sample project at the end of this tutorial.


*Randomize Numbers 1 to 10





 Private Sub number_timer_Tick(sender As Object, e As EventArgs) Handles number_timer.Tick
        Randomize()
        txtnumber.Text = CInt(Int((10 * Rnd()) + 0))
    End Sub

*Randomize an Array of Letters




 Private Sub word_timer_Tick(sender As Object, e As EventArgs) Handles word_timer.Tick
        Try
            Me.Cursor = Cursors.WaitCursor
            'count how many letters in the word entered
            Dim letter_counter As Integer
            letter_counter = Len(txtword.Text)
            'convert text to string
            Dim convert_string As String = txtword.Text
            'create an array of characters from the word
            Dim myChars() As Char = convert_string.ToCharArray
            'start randomizing characters
            Randomize()
            txtwordrandom.Text = myChars(CInt(Int((letter_counter * Rnd()) + 0)))
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

*Randomize More than 1 Set of Numbers to Generate a  Code



 Private Sub code_timer_Tick(sender As Object, e As EventArgs) Handles code_timer.Tick
        Dim str_code_1 As String
        Dim str_code_2 As String
        Dim str_code_3 As String
        Dim str_code_4 As String
        'code 1
        Randomize()
        str_code_1 = CInt(Int((10 * Rnd()) + 0))
        'code 2
        Randomize()
        str_code_2 = CInt(Int((10 * Rnd()) + 0))
        'code 3
        Randomize()
        str_code_3 = CInt(Int((10 * Rnd()) + 0))
        'code 4
        Randomize()
        str_code_4 = CInt(Int((10 * Rnd()) + 0))
        'generated code
        txtcode.Text = str_code_1 & str_code_2 & str_code_3 & str_code_4

    End Sub

*Select an Item from a Listbox using Randomization




 Private Sub list_timer_Tick(sender As Object, e As EventArgs) Handles list_timer.Tick
        Dim limit As Integer
        limit = lst_box_eat.Items.Count 'get total number of items to randomize
        Randomize()
        lst_box_eat.SelectedIndex = (CInt(Int((limit * Rnd()) + 0)))

    End Sub


*Randomize Collection of Images




   Private Sub pic_timer_Tick_1(sender As Object, e As EventArgs) Handles pic_timer.Tick
        Randomize()
        pic_img.BackgroundImage = img_lst.Images.Item(CInt(Int((7 * Rnd()) + 0)))
    End Sub

I guess you have an idea in your mind right after reading this. Download the complete working project below and try to work on it.

IDE: Visual Studio 2012

Project Type: Visual Basic Windows
Database: No Database







SHARE THIS POST

2 comments:

PRIVACY POLICY | TERMS OF SERVICE | COPYRIGHT POLICY