Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Posts
    19

    Unanswered: random number generator

    Hi all. I need some quick code that I can put into a text box area of a customer service script tool I use that will generate a random customer number everytime someone calls and goes to a certain page. I can put in html tags in the text box element so I am hopeing I can just put in the code for this and have it generate a random number that starts with "DC" plus a 4 digit number so each customer has a unique number, like DC001.
    Does anyone know of something quick in VB I can put into my customer service script that will generate this ??? Its a simple application that lets you make telemarketing scripts so you can read the pages to the customer, then jump to different choices based on a drop down. I know it supports VB code so I wanted to try to just hardcode the code into our "text box" element of this app.
    Can this be done with only a few lines ? I don't need anything fancy, just something to get me random customer #'s like DC001, DC002 etc...

  2. #2
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    Well, do you really want the numbers to be random, or just ascending? (the example you gave was an ascending number)

    Can there ever be duplicates? By its nature, from one number to the next, you can get duplicate "random" numbers. (for example, roll one die. You have an equal chance of getting any number between 1 and 6. Roll the die again. Again, you have an equal chance of getting a number between 1 and 6, even though you had already rolled a 2, for instance.) Or, do you want a list of unique numbers to be supplied in random order (like dealing from deck of cards, for instance?)

    Here's the code for an ascending function
    Code:
    Public Function AscendingFunction () as String
      Const Prefix as String = "DC"
      Static LastCount as Long
    
      LastCount = LastCount + 1
    
      AscendingFunction = Prefix & Right("0000" & cStr(LastCount), 4)
    
    End Function
    For a true random function, alter the function a bit (below.) Also, in the form's LOAD event, place the following line:

    Code:
    ' Place this code in the form's LOAD event.  (It should ONLY be called once in the life of your app.)
    Randomize
    Code:
    Public Function RandomFunction () as String
      Const Prefix as String = "DC"
      Const MaxLng as Long = 9999
      Dim NewCount as Long
    
      NewCount = cInt(rnd * MaxLong) + 1
    
      RandomFunction= Prefix & Right("0000" & cStr(NewCount ), 4)
    
    End Function
    The code to shuffle a series of numbers is a bit more involved. You would want to create an ordered array of numbers in the form load event, (with the array declared at the module level.) Immediately thereafter, you would shuffle the ordered array. Finally, in the shuffledFunction code, you would define the last position (as in the ascending function) as static, increment it at each call, and return the value of the shuffled array. at position LastCount.

    Ref this Random Numbers thread from our VB sister-site.
    Last edited by loquin; 12-19-07 at 18:12.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  3. #3
    Join Date
    Jul 2003
    Location
    Amsterdam, Nederland
    Posts
    450

    Thumbs up Random and unique

    Use a guid

  4. #4
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    A GUID could well be overkill, given his requirements.
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  5. #5
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    Not Guide.

    GUID. Globally Unique IDentifier
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •