random number generator

Posted on 2005-04-11
Medium Priority
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

rettiseert earned 600 total points
ID: 13758102
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

Assisted Solution

wraith821 earned 700 total points
ID: 13758127
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:


Expert Comment

ID: 13758136
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13758140
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)

Author Comment

ID: 13758151
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.


Accepted Solution

Dang123 earned 700 total points
ID: 13761655
    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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

809 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