Thread: Random Numbers/Letters

Random Numbers/Letters

From
"Hillensbeck, Preston"
Date:
Hello,

    This may have been discussed before(I have looked and could not find
much on this), but I need to find a way to generate random numbers and
letters together to insert into a field when a new entry is created.  Just
to give you a little insight, I will be accepting images from people and I
want to generate a random line of numbers or letters so that I can put a
name on a picture, and when someone pulls up a profile of the user, it will
pull the picture with the random name out of the directory.  I haven't been
able to find much on random number/letter generators within PostgreSQL so I
wanted to post here.  If anyone has an easy way for this to be done, please
let me know.  Thanks :)

Preston

Re: Random Numbers/Letters

From
Jeff Fitzmyers
Date:
>  I need to find a way to generate random numbers and
> letters together

I m no crytpo expert, but here is a php function I am toying with. The
reason I add the 200 characters is because I read somewhere that md5
'produces more random results' if it has a chunk to crunch. Maybe it is
inefficient, insecure or doesn't really add anything??

<?  // generate a random string
   function rand_string() {
   list($usec,$sec) = explode(" ", microtime());
   mt_srand($sec * $usec);
   $rand = mt_rand();
   $start = mt_rand(1,198);
   $length = mt_rand(1,198);
   // change these 400 characters often
   $text ='q54SMqa54VBPMqa54L3zxDKGzxs3zxswedWbbikplwlx
GAZX5Fy9hy7uMqn!aWE&ujNmaCVBPMqa54L3swedYeappCVBLd9c
vt9gbF!SazxsFDSagfHvt9gbFDSazMqKIF4cvxeEDh7ewSSKJTYe
dJMa5K54SWkGB3zxXCVazxsw989A5xswDS5FDS8nplavswAZXRC9
84cvfrTYeda5KJHG4Guv3321QN764r6yweswAZXR298lcvfraCxs
FDSaa5KJHG4321QN765Fz9hy7uMqnyaWEmitpl4MqvfhjcvytMzx
sw477as98j587UIJHqfrJHggrCVBLd9cjkGBMzxsw4ua52wm98j5
87UIJHqfrJHkjsNmAZzxDKGzxs7uj7ewr54AZXCVaz';
   $text = substr($text, $start, $length);
   $rand_s = md5($rand . $text);
   return $rand_s;
}


Re: Random Numbers/Letters

From
"Greg Sabino Mullane"
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> ... I need to find a way to generate random numbers and letters together
> to insert into a field when a new entry is created....
> If anyone has an easy way for this to be done, please let me know.

The easiest way of all would be to store the image inside the database
itself as a record, eliminating the need for random names entirely. Barring
that, however, your best bet is to have whatever application you are using
generate the random name. You will also have to check for duplicate entries
(perhaps by making the filename column UNIQUE), but collisions should be
very rare for any decent size (say, eight character) string of random
numbers and letters.

No telling what application you are using, but see man(2) mktemp for a
way to do it via a shell command, or do something like this in perl:

my @characters = ('a'..'z','A'..'Z',0..9);
my $ranstring = join('' => @characters[map {rand @characters}(1..8)]);
my $filename = "image_$ranstring.jpg";


Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200203151835

-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

iD8DBQE8koVyvJuQZxSWSsgRAuIaAJ0YxmJAOmz6hBTHFon6QSzMq4uDLgCgycJu
Wh4w+7WwFJnUcPc0tCXglBI=
=mJDO
-----END PGP SIGNATURE-----



Re: Random Numbers/Letters

From
Bruno Wolff III
Date:
On Thu, Mar 14, 2002 at 08:57:49AM -0600,
  "Hillensbeck, Preston" <PHillensbeck@sfbcic.com> wrote:
> Hello,
>
>     This may have been discussed before(I have looked and could not find
> much on this), but I need to find a way to generate random numbers and
> letters together to insert into a field when a new entry is created.  Just
> to give you a little insight, I will be accepting images from people and I
> want to generate a random line of numbers or letters so that I can put a
> name on a picture, and when someone pulls up a profile of the user, it will
> pull the picture with the random name out of the directory.  I haven't been
> able to find much on random number/letter generators within PostgreSQL so I
> wanted to post here.  If anyone has an easy way for this to be done, please
> let me know.  Thanks :)

I think you would be better off using a sequence instead. It seems what
you mostly want is a unique name. If you use random names, there is a chance
of a collision and this will make coding more complicated.