1

I'm working under MS-Visio 2010 in VBA (not an expert) and I want to generate a random number (several numbers would be even better) based on a string as seed.

I know that Rnd(seed) with seed as a negative number exists. However, I don't know about any random generator with a string as seed. Maybe some kind of hash function with a number as result ?

I'd like something like :

print function("abc")
45
print function("xyz abc-5")
86
print function("abc")
45

with spaces, symbols and numbers support when inside the seed string.

I may see a workaround by converting each character to some ascii number corresponding and somehow using this big number as seed with Rnd but it definitely feels far-fetched. Does anyone knows of a fancier way of doing so ?

Yvonnig
  • 69
  • 7
  • So do you want to create *random* number? Because as I see, you are trying to use it like a *hash*, so transform input string into some constant value, when for every same string you want to get same number output? – Van Ng Jun 20 '19 at 08:50
  • My goal here is to set the color of some shapes according to a string parameter (same string --> same color). I've already the part "random number --> random color", I miss the part "random string --> random number". But I don't really understand your question, hash is a case of random, isn't it ? – Yvonnig Jun 20 '19 at 08:59
  • Have a look at https://stackoverflow.com/questions/14717526/vba-hash-string, there are some examples to create Hashes – FunThomas Jun 20 '19 at 09:08
  • @Yvonnig hash is a function, that generates same number of fixed length for same input data. – Van Ng Jun 20 '19 at 09:36
  • @VanNg yes I understand that, but I mean it is still a function that takes a string and return a random number/string, with always the same result for the same parameter. But is there some hash functions in VBA that return always an integer ? Different for each different string entry ? – Yvonnig Jun 20 '19 at 12:20
  • 1
    "My goal here is to set the color of some shapes according to a string parameter (same string --> same color)." My advice here is to learn and love the Visio ShapeSheet, particularly the `Prop.` and `User.` fields. You can use these to create all sorts of visual magic without touching any VBA. www.visguy.com is a good early go-to to learn all sorts of stuff to make Visio sing. – AJD Jun 21 '19 at 00:04
  • @AJD thank you for the infos, however I need to do everything in VBA, I've potentially 1000+ Visio project with different input data but similar visualisation at the end, I can't do anything manually as it should be fully automated by the end. – Yvonnig Jun 21 '19 at 12:54
  • 1
    For your information, you can automate (through VBA) setting up/changes to ShapeSheets as well. And the benefits of using Masters in stencils with the automation set up in the ShapeSheet are also worth considering. this all comes down to planning your diagram(s), a little effort up front saves a lot of effort down the line when you amend the diagrams. – AJD Jun 21 '19 at 19:47

1 Answers1

2

Combined these examples

to:

    Function hash4(txt)
    ' copied from the example
    Dim x As Long
    Dim mask, i, j, nC, crc As Integer
    Dim c As String

    crc = &HFFFF

    For nC = 1 To Len(txt)
        j = Asc(Mid(txt, nC)) ' <<<<<<< new line of code - makes all the difference
        ' instead of j = Val("&H" + Mid(txt, nC, 2))
        crc = crc Xor j
        For j = 1 To 8
            mask = 0
            If crc / 2 <> Int(crc / 2) Then mask = &HA001
            crc = Int(crc / 2) And &H7FFF: crc = crc Xor mask
        Next j
    Next nC

    c = Hex$(crc)

    ' <<<<< new section: make sure returned string is always 4 characters long >>>>>
    ' pad to always have length 4:
    While Len(c) < 4
      c = "0" & c
    Wend

    Dim Hex2Dbl As Double

    Hex2Dbl = CDbl("&h0" & c) ' Overflow Error if more than 2 ^ 64
    If Hex2Dbl < 0 Then Hex2Dbl = Hex2Dbl + 4294967296# ' 16 ^ 8 = 4294967296
    hash4 = Hex2Dbl
   End Function

Try in immediate (Ctrl + G in VBA editor window):

?hash4("Value 1")
31335 
?hash4("Value 2") 
31527 

This function will:

  • return different number for different input strings
    • sometimes they will match, it is called hash-collisions
      • if it is critical, you can use md5, sha-1 hashes, their examples in VBA also available
  • return same number for same input strings
Van Ng
  • 773
  • 1
  • 7
  • 17