Question to create random number generator

jkhattar

Member
Joined
Jul 6, 2009
Messages
11
Programming Experience
Beginner
i need to create a random number genrator to generate a membership number when i open my sign up form. i need it to be shown in a text box. can someone help me please
 
I'm guessing that you do and maybe you just don't realise it. Have you ever created an object before, e.g.
VB.NET:
Dim myVariable As New SomeType
That's basically how you create an instance of any class, so if someone says "create an X object" you do that and simply specify the appropriate type. Have you ever called a method before, e.g.
VB.NET:
myVariable.MyMethod()
Same goes again. All methods are called the same way. You just need to pass the appropriate arguments to the method when you call it. Intellisense will tell you what those are as you type or, even better, you could read the documentation.
 
i have before yes but what i want is to have a random number generated into a text box and i want it to be able to only create that random number once meaning dont want it to create the same number twice. how do i go about this??
 
So, I'll assume that you know how to generate a random number and I'll also assume that you know how to display text in a TextBox as I'm sure you've done that before. That said, is a TextBox really appropriate? I'm guessing that you don't want the user to edit the value so would a Label maybe be better? You might also use a textBox and set its ReadOnly property to True.

With regards to ensuring that each number is unique, there are two ways you could go about it and which you use will depend on circumstances. One way is to create a list of all possible numbers first and then randomly select one of those and remove it from the list. That way, when you randomly select the next number, any that have already been removed cannot be selected.

The second option would be to generate a random number and then add it to a list. When you generate the next number you first check whether it is already in the list and, if so, you discard it and try again. This option is OK if the total number of possibilities is relatively small or the total number of selections is relatively small compared to the total number of possibilities. In other cases it can be a problem because you may end up wasting a lot of time generating and discarding values.

[ame="http://www.vbforums.com/showthread.php?t=393023"]Here[/ame]'s an example of the first option.
 
no infact i dont know how to generate a random number which is what im asking and i need to know how to create a unique one which can be displayed in a textbox. I dont think that the contents of a label can be saved in sql sever database can it?
 
So, have you done what I've told you so far? Have you created a Random object and called its Next method?

Whether or not you use a TextBox or a Label to display the data is irrelevant to saving data to the database. The Label and the TextBox both use the same Text property to display data but that's not what you would save to the database anyway. You have to have the data first in order to display it in a Label or a TextBox and it's that initial data that you should be saving to the database.
 
Member IDs should be saved to a database. If you don't it could have consequences later down the road.

Anyways, you need an application that gives someone a member ID once and never again?
Well, you'd need to somehow track this person, even after saving the member ID into a database. In the following example I will track them by their IP address. Meaning only one member ID per IP address.

First, follow this guide to get your mysql database connected to your VB.Net application:
CodeProject: VB.net to mySQL Database Connection. Free source code and programming help

The following code is untested, if you need more help with VB.Net and MySQL, I suggest this tutorial:
Accessing MySQL on VB.NET using MySQL Connector/Net, Part I: Introduction | Linglom's Blog

VB.NET:
Imports MySql.Data.MySqlClient
Imports System.Net

Class memberID

    'This function allows the member ID to be randomly generated.
    Public Function RandomNumber(ByVal MaxNumber As Integer, _
        Optional ByVal MinNumber As Integer = 0) As Integer
        Dim r As New Random(System.DateTime.Now.Millisecond)
        If MinNumber > MaxNumber Then
            Dim t As Integer = MinNumber
            MinNumber = MaxNumber
            MaxNumber = t
        End If
        Return r.Next(MinNumber, MaxNumber)
    End Function

    'This allows the generated memberid to be used outside of the sub it generates it in.
    Dim memberidrand As Integer

    'This uses the above function generate a random number between 100,000 and 999,999
    Sub getid()
        Dim memberidrand = RandomNumber(100000, 999999)
    End Sub

    'This defines variables for use for the code below.
    Dim localHost As String
    Dim checkRecord As String
    Dim NoMore As Integer = 0

    'This sub is used to get the member ID and put it in the database. There are errors here because MySQL Queries are executed incorrectly inside this sub.
    Sub getmembid()
        'localHost will get the current person's host name to make sure they don't already have a member ID.
        localHost = Dns.GetHostName()
        Console.WriteLine(checkIP("SELECT IP FROM allmemberidtable WHERE IP = " & localHost))
        'This If checks to make sure that their host name is not in the database.
        If checkIP = "" Then
            getid()
            Dim conn = New MySqlConnection()
            conn.ConnectionString = "server=x.x.x.x; user id=root; password=xxx; database=xxx"
            conn.Open()
            'This puts their host name into the database.
            Console.WriteLine(updateRecordIP("INSERT INTO allmemberidtable VALUES (IP) " & localHost))
            'This query checks to make sure the randomly generated member ID isn't already in the database.
            Console.WriteLine(checkRecord("SELECT memberid FROM allmemberidtable WHERE memberid = " & memberidrand))
        Else
            'This should only happen if the person already has a member ID. It will tell the code to stop working and gives them their current member ID.
            NoMore = 1
            Console.WriteLine(getMemberID("SELECT memberid FROM allmemberidtable WHERE IP = " & localHost))
            MsgBox("Your member ID is: " & getMemberID)
        End If
    End Sub

    'This is what happens when they click the main button.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'This runs the above sub to check for their host name and duplicates of the randomly generated member ID.
        getmembid()
        'This uses the variable set in the above sub to make sure that the member id is not duplicated.
        If checkRecord = "" Then
            'This puts their member ID into the database.
            Console.WriteLine(updateRecord("INSERT INTO allmemberidtable VALUES (memberid) " & memberidrand))
        Else
            'If it is duplicated and their host name isn't in the database, it makes a new member ID.
            If NoMore = 0 Then
                getmembid()
            End If
        End If
    End Sub
End Class

The code is commented out to help you as much as possible. Comments are the text written after the apostrophes ('). Copy paste this code into VB.Net 2008 to work with it. Make sure to use the two above tutorials!

Please note this code is untested and already has errors because MySQL Queries were executed incorrectly in VB.Net.
 
Last edited:
Back
Top