On October 16, 2009 12:29:42 pm Jerry Schwartz wrote:
> >-----Original Message-----
> >From: Ray [mailto:r...@stilltech.net]
> >Sent: Friday, October 16, 2009 1:10 PM
> >To: mysql@lists.mysql.com
> >Subject: Re: insert random number into table
> >
> >On October 16, 2009 10:57:48 am Jerry Schwartz wrote:
> >> There is a RAND function in MySQL, but if you need to guarantee that the
> >> identifiers are unique you should consider using an auto-increment
> >> field.
> >>
> >> If that isn't unique enough, you can use the UUID or UUID_SHORT
> >> functions. Theoretically, those should return values that are unique
> >> across the Internet (at least).
> >>
> >> Regards,
> >>
> >> Jerry Schwartz
> >> The Infoshop by Global Information Incorporated
> >> 195 Farmington Ave.
> >> Farmington, CT 06032
> >>
> >> 860.674.8796 / FAX: 860.674.8341
> >>
> >> www.the-infoshop.com
> >>
> >> >-----Original Message-----
> >> >From: Ray [mailto:r...@stilltech.net]
> >> >Sent: Friday, October 16, 2009 11:43 AM
> >> >To: mysql@lists.mysql.com
> >> >Subject: insert random number into table
> >> >
> >> >Hello All,
> >> >I am trying to insert a unique random number* with each row when
> >> > inserting into a table. I can do it in the application layer (php) but
> >> > just wondering if
> >> >there is an easier way in the database layer.
> >> >Thanks
> >> >Ray
> >> >
> >> >* for example, when registering a person for a contest, I want to give
> >> > them a unique, random identifier.
> >> >
> >> >--
> >> >MySQL General Mailing List
> >> >For list archives: http://lists.mysql.com/mysql
> >> >To unsubscribe:    http://lists.mysql.com/mysql?unsub=jschwa...@the-
> >> >infoshop.com
> >
> >Thanks for the replies. I think I'll use the php solution as it is the
> > better match for what I am trying to do. I'l keep the UUID functions in
> > mind for future use though.
> >Ray
>
> [JS] Just remember that the pseudo-random numbers that come out of a
> computer, regardless of where or how they are generated, are not guaranteed
> to be unique. Although the odds of a collision are slim, my philosophy has
> always been "if it's possible, it will happen - in the middle of your
> vacation".
>
> If you want to generate UUIDs in PHP, take a look at
> http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/
>
> >--
> >MySQL General Mailing List
> >For list archives: http://lists.mysql.com/mysql
> >To unsubscribe:    http://lists.mysql.com/mysql?unsub=jschwa...@the-
> >infoshop.com


Thanks for all the suggestions and points. There are definitely some I will 
keep in mind for future projects. auto increment won't work because I don't 
want the number to be based on the order of assignment.
I probably should have been more clear. What I am trying to do is randomly 
assign one of a fixed pool of numbers uniquely to each row. 
What I ended up doing is (in PHP): 
SELECT all used numbers and put them in an array.
create an array of all allowed numbers. 
combine the two arrays such that all available numbers are left in a third 
array with index numbers from 1 counting up sequentially. I then randomly 
choose one of those numbers and write it to the table.
Thanks again for all your suggestions.
Ray

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=arch...@jab.org

Reply via email to