random number generator

Hello,

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.
LVL 1
rite_ehAsked:
Who is Participating?
 
Dang123Connect With a Mentor Commented:
rite_eh,
    Here is a simple hash function you may adapt

Hashing (One Way Encryption) Algorithm
http://www.freevbcode.com/ShowCode.Asp?ID=972



Or perhaps using the first 7 characters of a GUID

http://www.freevbcode.com/ShowCode.Asp?ID=21

Dang123
0
 
rettiseertConnect With a Mentor Commented:
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
0
 
wraith821Connect With a Mentor Commented:
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:
http://www.vbwm.com/forums/topic.asp?ARCHIVE=true&TOPIC_ID=2351

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
wraith821Commented:
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.
0
 
rite_ehAuthor Commented:
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)
0
 
rite_ehAuthor Commented:
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.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.