Generate random string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Meade

    Generate random string

    Hi all,

    I need to be able to create some license keys for an application. Ideally
    these wouldn't be too long in length (ie, easier to remember/type in), but I
    would like them to be auto generated and a check made to see whether or not
    that key has already been used, something which could generate up to about
    10000 keys (or more if the string doesnt have to be too long) would be very
    useful.

    A combination of letters, numbers, upper and lower case is needed.

    I could sit here and start plugging away at this, an array for numbers, an
    array for letters, a random test for uppercase/lowercase etc, but it occured
    to me that I'm probably just reinventing the wheel a bit here, someone must
    have done this before!

    Has anyone produced anything similar to this in the past?

    Any help would be appreciated.

    Regards

    Rob


  • Aaron [SQL Server MVP]

    #2
    Re: Generate random string

    Why not just use a GUID?

    --

    (Reverse address to reply.)




    "Rob Meade" <robb.meade@N O-SPAM.kingswoodw eb.net> wrote in message
    news:9nIxc.2252 $PQ.20197614@ne ws-text.cableinet. net...[color=blue]
    > Hi all,
    >
    > I need to be able to create some license keys for an application. Ideally
    > these wouldn't be too long in length (ie, easier to remember/type in), but[/color]
    I[color=blue]
    > would like them to be auto generated and a check made to see whether or[/color]
    not[color=blue]
    > that key has already been used, something which could generate up to about
    > 10000 keys (or more if the string doesnt have to be too long) would be[/color]
    very[color=blue]
    > useful.
    >
    > A combination of letters, numbers, upper and lower case is needed.
    >
    > I could sit here and start plugging away at this, an array for numbers, an
    > array for letters, a random test for uppercase/lowercase etc, but it[/color]
    occured[color=blue]
    > to me that I'm probably just reinventing the wheel a bit here, someone[/color]
    must[color=blue]
    > have done this before!
    >
    > Has anyone produced anything similar to this in the past?
    >
    > Any help would be appreciated.
    >
    > Regards
    >
    > Rob
    >
    >[/color]


    Comment

    • Dave Anderson

      #3
      Re: Generate random string

      Rob Meade wrote:[color=blue]
      >
      > I need to be able to create some license keys for an application.
      > Ideally these wouldn't be too long in length (ie, easier to
      > remember/type in), but I would like them to be auto generated and a
      > check made to see whether or not that key has already been used,
      > something which could generate up to about 10000 keys (or more if the
      > string doesnt have to be too long) would be very useful.
      >
      > A combination of letters, numbers, upper and lower case is needed.
      >
      > I could sit here and start plugging away at this, an array for
      > numbers, an array for letters, a random test for uppercase/lowercase
      > etc, but it occured to me that I'm probably just reinventing the
      > wheel a bit here, someone must have done this before!
      >
      > Has anyone produced anything similar to this in the past?[/color]

      This will do far more than 10,000, and will give you uniqueness:

      GUID = Server.CreateOb ject("Scriptlet .Typelib").GUID




      --
      Dave Anderson

      Unsolicited commercial email will be read at a cost of $500 per message. Use
      of this email address implies consent to these terms. Please do not contact
      me directly or ask me to contact you directly for assistance. If your
      question is worth asking, it's worth posting.


      Comment

      • Rob Meade

        #4
        Thanks - but how will it..

        Thanks chaps, but how will it know its 'unique'? ie, how will it remember
        that it hasnt already used that one? I'll try the code in a minute or two -
        but if you happen to reply prior to that - how many characters does this
        generate by the way?

        Thanks again,

        Rob


        Comment

        • Rob Meade

          #5
          Re: Thanks - but how will it..

          Ok - so I got this:


          Comment

          • Rob Meade

            #6
            Re: Thanks - but how will it..

            (I hate ctrl+enter!!).. .

            I got this...

            FFDA9960-8C53-497F-9D8F-A736352F21C0

            That and a password is what a user will need to enter when logging into a
            members area to download any updates to an application - bit long - hard to
            remember etc...(great for uniqueness mind!)...

            Thoughts/suggestions...

            Rob


            Comment

            • Aaron [SQL Server MVP]

              #7
              Re: Thanks - but how will it..

              Well, GUID stands for Globally Unique IDentifier, so you can do the math
              from there...

              Keep this in mind, of course, if you are going to generate or response.write
              from ASP:


              --

              (Reverse address to reply.)




              "Rob Meade" <robb.meade@N O-SPAM.kingswoodw eb.net> wrote in message
              news:u7Jxc.6$bD 1.288991@news-text.cableinet. net...[color=blue]
              > Thanks chaps, but how will it know its 'unique'? ie, how will it remember
              > that it hasnt already used that one? I'll try the code in a minute or[/color]
              two -[color=blue]
              > but if you happen to reply prior to that - how many characters does this
              > generate by the way?
              >
              > Thanks again,
              >
              > Rob
              >
              >[/color]


              Comment

              • Aaron [SQL Server MVP]

                #8
                Re: Thanks - but how will it..

                Why does a serial # have to be easy to remember? Do you remember your
                Windows product key, your Photoshop serial number, hell your credit card
                number?

                If you want something easy to remember, then toss this random string
                generation out the window, and make users choose their own password!

                --

                (Reverse address to reply.)




                "Rob Meade" <robb.meade@N O-SPAM.kingswoodw eb.net> wrote in message
                news:S9Jxc.8$NF 1.343296@news-text.cableinet. net...[color=blue]
                > (I hate ctrl+enter!!).. .
                >
                > I got this...
                >
                > FFDA9960-8C53-497F-9D8F-A736352F21C0
                >
                > That and a password is what a user will need to enter when logging into a
                > members area to download any updates to an application - bit long - hard[/color]
                to[color=blue]
                > remember etc...(great for uniqueness mind!)...
                >
                > Thoughts/suggestions...
                >
                > Rob
                >
                >[/color]


                Comment

                • Rob Meade

                  #9
                  Re: Thanks - but how will it..

                  "Aaron [SQL Server MVP]" wrote ...
                  [color=blue]
                  > Why does a serial # have to be easy to remember?[/color]

                  Because they'll be using it often for logging in purposes, hence in my first
                  post suggesting that it didnt have too many characters.
                  [color=blue]
                  > Do you remember your Windows product key, your Photoshop serial number,[/color]

                  Absolutely not - agree fully with you there, but I dont need to enter those
                  every time I want to download a patch/fix etc..
                  [color=blue]
                  > hell your credit card number?[/color]

                  That one I do know - but my card kinda glows red so its easy to see :o)
                  [color=blue]
                  > If you want something easy to remember, then toss this random string
                  > generation out the window, and make users choose their own password![/color]

                  They'll have a password as well as the key Aaron, the licence number I guess
                  is affectively their username.

                  Regards

                  Rob


                  Comment

                  • Rob Meade

                    #10
                    Re: Thanks - but how will it..

                    "Aaron [SQL Server MVP]" wrote ...
                    [color=blue]
                    > Well, GUID stands for Globally Unique IDentifier, so you can do the math
                    > from there...[/color]

                    Whilst not really wanting to go down this road, but feeling propelled to say
                    it....there must be an upper limit to the number of combinations though
                    surely - if I do the response.write GUID for example and then held F5 down
                    on my PC lets say for the rest of my life time I'm surely going to get a
                    duplicate eventually???

                    I'm unaware (and you might have to excuse my ignorance a little as Maths was
                    never a strong subject for me) - of anything thats simply endless - apart
                    from space - but even that must have boundaries somewhere...(wo oow - brain
                    is starting to melt a bit now)..
                    [color=blue]
                    > Keep this in mind, of course, if you are going to generate or[/color]
                    response.write[color=blue]
                    > from ASP:
                    > http://www.aspfaq.com/2358[/color]

                    Thanks for that, I didnt even know about the GUID ability in ASP so I've
                    learnt something new tonight :o)

                    Rob


                    Comment

                    • Aaron [SQL Server MVP]

                      #11
                      Re: Thanks - but how will it..

                      > They'll have a password as well as the key Aaron, the licence number I
                      guess[color=blue]
                      > is affectively their username.[/color]

                      Then I suggest using their e-mail address as the username (which is actually
                      quite common). *Anything* you generate for them is going to be another
                      hard-to-remember string that they're going to write on a sticky, forget,
                      etc.

                      A


                      Comment

                      • Aaron [SQL Server MVP]

                        #12
                        Re: Thanks - but how will it..

                        The number of combinations is 36 ^ 32. Think about this for a second... 36
                        ^ 6 exceeds the upper boundary for an INT datatype (~2 billion). 36 ^ 13
                        exceeds the upper boundary for a BIGINT datatype
                        (9,223,372,036, 854,775,807). So take that latter number, square it, and
                        then multiply that by 2 billion.

                        This is no RAND(), trust me.

                        --

                        (Reverse address to reply.)




                        "Rob Meade" <robb.meade@N O-SPAM.kingswoodw eb.net> wrote in message
                        news:jNJxc.53$i 12.852173@news-text.cableinet. net...[color=blue]
                        > "Aaron [SQL Server MVP]" wrote ...
                        >[color=green]
                        > > Well, GUID stands for Globally Unique IDentifier, so you can do the math
                        > > from there...[/color]
                        >
                        > Whilst not really wanting to go down this road, but feeling propelled to[/color]
                        say[color=blue]
                        > it....there must be an upper limit to the number of combinations though
                        > surely - if I do the response.write GUID for example and then held F5 down
                        > on my PC lets say for the rest of my life time I'm surely going to get a
                        > duplicate eventually???
                        >
                        > I'm unaware (and you might have to excuse my ignorance a little as Maths[/color]
                        was[color=blue]
                        > never a strong subject for me) - of anything thats simply endless - apart
                        > from space - but even that must have boundaries somewhere...(wo oow - brain
                        > is starting to melt a bit now)..
                        >[color=green]
                        > > Keep this in mind, of course, if you are going to generate or[/color]
                        > response.write[color=green]
                        > > from ASP:
                        > > http://www.aspfaq.com/2358[/color]
                        >
                        > Thanks for that, I didnt even know about the GUID ability in ASP so I've
                        > learnt something new tonight :o)
                        >
                        > Rob
                        >
                        >[/color]


                        Comment

                        • Evertjan.

                          #13
                          Re: Thanks - but how will it..

                          Aaron [SQL Server MVP] wrote on 09 jun 2004 in
                          microsoft.publi c.inetserver.as p.general:
                          [color=blue]
                          > The number of combinations is 36 ^ 32. Think about this for a
                          > second... 36 ^ 6 exceeds the upper boundary for an INT datatype (~2
                          > billion). 36 ^ 13 exceeds the upper boundary for a BIGINT datatype
                          > (9,223,372,036, 854,775,807). So take that latter number, square it,
                          > and then multiply that by 2 billion.
                          >
                          > This is no RAND(), trust me.
                          >[/color]

                          rant() perhaps?

                          Main Entry: rant
                          Pronunciation: 'rant
                          Function: verb
                          Etymology: obsolete Dutch ranten, randen
                          intransitive senses
                          1 : to talk in a noisy, excited, or declamatory manner
                          2 : to scold vehemently
                          transitive senses : to utter in a bombastic declamatory fashion

                          =============== =

                          But then, Aaron, we trust you.

                          --
                          Evertjan.
                          The Netherlands.
                          (Please change the x'es to dots in my emailaddress)

                          Comment

                          • Aaron [SQL Server MVP]

                            #14
                            Re: Thanks - but how will it..

                            > 1 : to talk in a noisy, excited, or declamatory manner[color=blue]
                            > 2 : to scold vehemently
                            > transitive senses : to utter in a bombastic declamatory fashion
                            >
                            > =============== =
                            >
                            > But then, Aaron, we trust you.[/color]

                            I don't think I have submitted examples of either 1. or 2. in this thread.
                            It's pretty simple math, really.

                            A


                            Comment

                            • Dave Anderson

                              #15
                              Re: Thanks - but how will it..

                              Aaron [SQL Server MVP] wrote:[color=blue][color=green]
                              >> ...surely - if I do the response.write GUID for example and then
                              >> held F5 down on my PC lets say for the rest of my life time I'm
                              >> surely going to get a duplicate eventually???
                              >>[/color]
                              > The number of combinations is 36 ^ 32. Think about this for a
                              > second... 36 ^ 6 exceeds the upper boundary for an INT datatype (~2
                              > billion). 36 ^ 13 exceeds the upper boundary for a BIGINT datatype
                              > (9,223,372,036, 854,775,807). So take that latter number, square it,
                              > and then multiply that by 2 billion.[/color]

                              To be fair, his computer cannot generate all 36^32 values, as he has a very
                              small subset of the MAC address space. Holding down F5 would run him through
                              his remaining number of timestamps, but certainly not the entire GUID space.



                              --
                              Dave Anderson

                              Unsolicited commercial email will be read at a cost of $500 per message. Use
                              of this email address implies consent to these terms. Please do not contact
                              me directly or ask me to contact you directly for assistance. If your
                              question is worth asking, it's worth posting.


                              Comment

                              Working...