Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. random unique number [mysql]

random unique number [mysql]

Scheduled Pinned Locked Moved Database
questionmysqllounge
18 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    how can I generate a random unique Int64 number in the activation_number field but I want to make sure the generated number doesn't exist in the table?

    B L D S P 6 Replies Last reply
    0
    • J Jassim Rahma

      how can I generate a random unique Int64 number in the activation_number field but I want to make sure the generated number doesn't exist in the table?

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      select sum(activation_number)+1 from mytable


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

      L 1 Reply Last reply
      0
      • B Blue_Boy

        select sum(activation_number)+1 from mytable


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        that will not work well, unless the table is extremely short. 1, 2, 4, 8, 16, ..., 2<<31, 0, 1, 2, 4, ... :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        L 1 Reply Last reply
        0
        • J Jassim Rahma

          how can I generate a random unique Int64 number in the activation_number field but I want to make sure the generated number doesn't exist in the table?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          That's "generating a number" and "verify it's occurance". For that you'd need; An efficient way of generating a number An efficient way of looking up a number (index the field!!) Or, more efficient, use an auto-incremented[^] column. Isn't type BigInt allowed in MySql? Something like below, if I adapted the example correctly;

          CREATE TABLE animals
          (
          id BIGINT NOT NULL AUTO_INCREMENT,
          name CHAR(30) NOT NULL,
          PRIMARY KEY (id)
          ) ENGINE=MyISAM;

          I are Troll :suss:

          L 1 Reply Last reply
          0
          • L Luc Pattyn

            that will not work well, unless the table is extremely short. 1, 2, 4, 8, 16, ..., 2<<31, 0, 1, 2, 4, ... :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I are Troll :suss:

            1 Reply Last reply
            0
            • L Lost User

              That's "generating a number" and "verify it's occurance". For that you'd need; An efficient way of generating a number An efficient way of looking up a number (index the field!!) Or, more efficient, use an auto-incremented[^] column. Isn't type BigInt allowed in MySql? Something like below, if I adapted the example correctly;

              CREATE TABLE animals
              (
              id BIGINT NOT NULL AUTO_INCREMENT,
              name CHAR(30) NOT NULL,
              PRIMARY KEY (id)
              ) ENGINE=MyISAM;

              I are Troll :suss:

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Now what happened to GUIDs? and where are PIEBALD's sequences? he isn't building a snowman, is he? we need him here. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              L P 2 Replies Last reply
              0
              • L Luc Pattyn

                Now what happened to GUIDs? and where are PIEBALD's sequences? he isn't building a snowman, is he? we need him here. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Sequences? Isn't that Oracle? There's an autoincrement-column in MySql, sounded logical to me to abuse that. He's probably setting up the different components for an extendible snowman :)

                I are Troll :suss:

                L P 2 Replies Last reply
                0
                • L Lost User

                  Sequences? Isn't that Oracle? There's an autoincrement-column in MySql, sounded logical to me to abuse that. He's probably setting up the different components for an extendible snowman :)

                  I are Troll :suss:

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  I was hinting at an earlier thread, found it here[^]. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Now what happened to GUIDs? and where are PIEBALD's sequences? he isn't building a snowman, is he? we need him here. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Actually, I was at a play rehearsal. :-O It's good to be the king. :cool: I did see the post earlier, but I didn't feel like chiming in. Plus, he said "random", so I didn't think a sequence would work. I suppose one might be able to generate a GUID and use only 64 bits of it; RowID , convert to char , substring , convert to big int... X|

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      Sequences? Isn't that Oracle? There's an autoincrement-column in MySql, sounded logical to me to abuse that. He's probably setting up the different components for an extendible snowman :)

                      I are Troll :suss:

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Eddy Vluggen wrote:

                      extendible snowman

                      extensible !! :-D A snowman from which all other snowfolk can be derived? Maybe I should look into a Turing Snowman or an Analytical Snowman. :cool:

                      L 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Actually, I was at a play rehearsal. :-O It's good to be the king. :cool: I did see the post earlier, but I didn't feel like chiming in. Plus, he said "random", so I didn't think a sequence would work. I suppose one might be able to generate a GUID and use only 64 bits of it; RowID , convert to char , substring , convert to big int... X|

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        Thank you, your majesty. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          Eddy Vluggen wrote:

                          extendible snowman

                          extensible !! :-D A snowman from which all other snowfolk can be derived? Maybe I should look into a Turing Snowman or an Analytical Snowman. :cool:

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Buy a large cooler, otherwise it'll overheat during parallel tasks :)

                          I are Troll :suss:

                          1 Reply Last reply
                          0
                          • J Jassim Rahma

                            how can I generate a random unique Int64 number in the activation_number field but I want to make sure the generated number doesn't exist in the table?

                            D Offline
                            D Offline
                            dbaechtel
                            wrote on last edited by
                            #13

                            If you want to make sure it is unique, I would suggest you use a GUID instead on Int64.

                            1 Reply Last reply
                            0
                            • J Jassim Rahma

                              how can I generate a random unique Int64 number in the activation_number field but I want to make sure the generated number doesn't exist in the table?

                              S Offline
                              S Offline
                              Snowman58
                              wrote on last edited by
                              #14

                              True Random number generation is an art - most computer generated random numbers are actually periodic and not truly random. But usually "good enough". See this site for a discussion of a few truly random number techniques. http://www.random.org/randomness/[^] They also offer an API to generate true random numbers.

                              Melting Away www.deals-house.com www.innovative--concepts.com

                              1 Reply Last reply
                              0
                              • J Jassim Rahma

                                how can I generate a random unique Int64 number in the activation_number field but I want to make sure the generated number doesn't exist in the table?

                                P Offline
                                P Offline
                                Pcube
                                wrote on last edited by
                                #15

                                If you're assigning a random number to every entry in the table (i.e. every single time you add an entry you associate a random number with it) then a good mechanism to use is the LFSR - Linear Feedback Shift Register. An n-bit LFSR will iterate through 2^n numbers in a fixed (deterministic) sequence (thus, not very random, but it might suit your purposes for uniqueness) and guarantees to loop back to the first number outputted after 2^n iterations. I'm a little rusty on the details, but it should definitely guarantee that you don't exhaust every number for a "long" time. Xilinx has a good app note on this: http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf[^]

                                1 Reply Last reply
                                0
                                • J Jassim Rahma

                                  how can I generate a random unique Int64 number in the activation_number field but I want to make sure the generated number doesn't exist in the table?

                                  A Offline
                                  A Offline
                                  AspDotNetDev
                                  wrote on last edited by
                                  #16

                                  Assuming you don't want to use a GUID... Create a table of number ranges; call it RandRanges. These are the numbers that are not yet taken. Generate a random integer (use the Rand function along with some multiplication and addition) between 1 and the number of items in RandRanges. Use that number to select a row from RandRanges. Use the min/max values (i.e., the range) as the min/max of a random integer you will generate. Once you generate that integer, use that number as your desired random number. Then, modify the RandRanges table to take that value out. You can do that by deleting the row you just created and splitting it into two new ranges that you insert into the table. For example, say RandRanges starts out with:

                                  1: 1-100
                                  2: 101-200

                                  You then generate the random number 2, so you are gonna use the second item in RandRanges.

                                  101-200

                                  Now, you generate a random number between 101 and 200. Say that turns out to be 150. You would take that out of RandRanges:

                                  1:1-100
                                  2:101-149
                                  3:151-200

                                  You then use your unique random number, 150, however you like. This table will grow at about the same rate that you use the unique random numbers, so you don't have to worry too much about space considerations. In fact, the table will sometimes shrink. If you use up all the random numbers (not likely with an Int64 range), the table would become empty.

                                  [Forum Guidelines]

                                  U 1 Reply Last reply
                                  0
                                  • A AspDotNetDev

                                    Assuming you don't want to use a GUID... Create a table of number ranges; call it RandRanges. These are the numbers that are not yet taken. Generate a random integer (use the Rand function along with some multiplication and addition) between 1 and the number of items in RandRanges. Use that number to select a row from RandRanges. Use the min/max values (i.e., the range) as the min/max of a random integer you will generate. Once you generate that integer, use that number as your desired random number. Then, modify the RandRanges table to take that value out. You can do that by deleting the row you just created and splitting it into two new ranges that you insert into the table. For example, say RandRanges starts out with:

                                    1: 1-100
                                    2: 101-200

                                    You then generate the random number 2, so you are gonna use the second item in RandRanges.

                                    101-200

                                    Now, you generate a random number between 101 and 200. Say that turns out to be 150. You would take that out of RandRanges:

                                    1:1-100
                                    2:101-149
                                    3:151-200

                                    You then use your unique random number, 150, however you like. This table will grow at about the same rate that you use the unique random numbers, so you don't have to worry too much about space considerations. In fact, the table will sometimes shrink. If you use up all the random numbers (not likely with an Int64 range), the table would become empty.

                                    [Forum Guidelines]

                                    U Offline
                                    U Offline
                                    User 4061826
                                    wrote on last edited by
                                    #17

                                    Perhaps try to redefine the problem a bit. For example, if it's an activation code then you can make it out of two parts: one sequential part, and a random part. If the sequential part is unique, the activation code will be unique. Of course the drawback is that you can issue less activation codes but I do not imagine that you want to activate 2^64 whatevers :)

                                    A 1 Reply Last reply
                                    0
                                    • U User 4061826

                                      Perhaps try to redefine the problem a bit. For example, if it's an activation code then you can make it out of two parts: one sequential part, and a random part. If the sequential part is unique, the activation code will be unique. Of course the drawback is that you can issue less activation codes but I do not imagine that you want to activate 2^64 whatevers :)

                                      A Offline
                                      A Offline
                                      AspDotNetDev
                                      wrote on last edited by
                                      #18

                                      Uh, I think you meant to post that as a reply to the OP's message and not as a reply to mine.

                                      [Forum Guidelines]

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups