random number generator

Posted on 2005-04-11
Last Modified: 2010-05-02

I need to have a vb function that I can call which will return a 7 digit hash or random and unique number, based on a string it is passed. This number must be unique and could be calculated using the string itself or any other method, such as the date and time for example. If necessary the string can be limited to a fixed number or characters, within reason (perhaps 40 or 50 characters). I have already written a routine which simply returns the current time and first digit of the day (DHHMMSS) however this same number could be assigned to another element in the future. Checking to see if a number is already in use is possible, but I would prefer to avoid it since these numbers are being outputted to a flat file.

Thanks in advance for any suggestions or code.
Question by:rite_eh
    LVL 13

    Assisted Solution

    What about just using simple randomize and rnd functions?

    Private Sub Form_Load()

        Randomize Timer           'Call this when your program start
        MsgBox get7DigitsRandomNumber
    End Sub

    Private Function get7DigitsRandomNumber() As Long
        Dim upperbound As Long, lowerbound As Long
        upperbound = 9999999
        lowerbound = 1000000
        get7DigitsRandomNumber = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    End Function
    LVL 8

    Assisted Solution

    if it is only 7 numbers, then you only have 0000000 - 9999999 if that is an acceptable range, then you could create a Registry Entry with the last number used and just get that number and increment one saving to the registry each time.

    Dim MyHash as Long
    MyHash = clng(GetSetting ("Appname","Settings","IndexHash",0 )) +1
    '*** assign your number code here ***
    SaveSetting "Appname","Settings","IndexHash",MyHash

    if you need more numbers you could use keyboard characters and create a base36 or even maybe a Base108 (or as many valid single characters as on the keyboard, including ~!@#$%^&*()_+=-][}{'";:/?.>,<\|) you could use upper and lower case characters.

    maybe this can help also:

    LVL 8

    Expert Comment

    if the application needs to be run from more then one machine then you could save the number in a file, or some other common location.
    LVL 1

    Author Comment

    The problem is that there is still a small a chance a number could be returned more than once. But then again I suppose even a hash of the value could potentially have a collission, although I would expect it is less likely (especially if I disallow adding elements with the same name)
    LVL 1

    Author Comment

    wraith821, I like the idea of using a different base. Thanks for the link. I'll leave this question open until tomorrow and then divide the points up between all the people offering suggestions and code.

    LVL 9

    Accepted Solution

        Here is a simple hash function you may adapt

    Hashing (One Way Encryption) Algorithm

    Or perhaps using the first 7 characters of a GUID


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now