random unique number [mysql]
-
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?
-
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?
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
-
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
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.
-
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?
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:
-
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.
-
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:
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.
-
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.
-
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:
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.
-
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.
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|
-
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:
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:
-
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|
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.
-
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:
-
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?
-
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?
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
-
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?
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[^]
-
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?
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-200You 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-200You 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.
-
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-200You 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-200You 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.
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 :)
-
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 :)
Uh, I think you meant to post that as a reply to the OP's message and not as a reply to mine.