Author Topic: Random auto increment ID  (Read 4392 times)

0 Members and 1 Guest are viewing this topic.

Offline Paul

  • Hero Member
  • *****
  • Posts: 162
Random auto increment ID
« on: April 18, 2007, 04:29:56 AM »
Here is a little tip for creating random auto increments with csvwrite.

It is useful if you allow multiple users to search your database using the ID number.

If you don't want these users searching other peoples ID numbers (i.e. 1 above or 1 below their ID) why not make it difficult / tedious for them to do so by adding a little randomness!

Find  $maxID++;  in csvwrite.pl & put a hash in front of it like this.

# $maxID++;

Then copy and paste the following code directly underneath it like this.
######################################

# $maxID++;

srand();     #mod to create random auto increment
$maxID=$maxID + 10 + int(rand(101));

#######################################

What this is doing is taking the previous maximum ID number in your database and adding a random number between 0 and less than 101 (i.e. between 0-100).

Just to cover all bases, there is a chance that zero "0" could be picked as the random number, that's why you should add another value (in this case 10), just to make sure that the minumum ID number difference would be at least 10.

You can change the 10 or 101 values to anything you want.

Hope it's useful for somebody...I've been using this for a while, so it's tried and tested.






Offline Jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 1018
    • slipdex
Re: Random auto increment ID
« Reply #1 on: April 18, 2007, 06:53:52 AM »
Paul,

NICE WORK!!  Quick question... The script will still start at 1000000 right?  And if so, might it be logically possible, using your example numbers, for the script to write two ID's with the same number?  Or will the script automatically reject a duplicate ID attempt?

Jason
Founder of www.slipdex.com

Offline Paul

  • Hero Member
  • *****
  • Posts: 162
Re: Random auto increment ID
« Reply #2 on: April 18, 2007, 07:42:29 AM »
The script will keep adding on to the previous ID number Jason.

So if the database ID started at say 1000, and the random numbers generated by the script were 31 for one data entry and 52 for another, the ID's would be

1000|more fields| ....
1031|more fields| ....
1083|more fields| ....

Offline Jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 1018
    • slipdex
Re: Random auto increment ID
« Reply #3 on: April 18, 2007, 09:56:59 AM »
That is SPECTACULAR Paul!!  Great hack!!  Makes perfect sense to me now.  Don't know why it didn't the first time I read it, but now...  sank right in! haha

Jason
Founder of www.slipdex.com

Offline EZ

  • Hero Member
  • *****
  • Posts: 1081
    • Babelnotes.be
Re: Random auto increment ID
« Reply #4 on: April 18, 2007, 10:24:03 AM »
Nice hack  ;)

Offline smc

  • Full Member
  • ***
  • Posts: 45
    • EternityWhere.com
Re: Random auto increment ID
« Reply #5 on: April 18, 2007, 01:56:34 PM »
Possible a useful way to generate random passwords!  Nice one!

Offline Jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 1018
    • slipdex
Re: Random auto increment ID
« Reply #6 on: May 21, 2007, 07:42:14 AM »
Just added this hack in and it works famously!!!  Great job Paul!

Jason
Founder of www.slipdex.com

Offline Paul

  • Hero Member
  • *****
  • Posts: 162
Re: Random auto increment ID
« Reply #7 on: September 24, 2007, 04:08:53 AM »
No problem Jason,
Here is one more hack to produce alpha-numeric ID's

This will produce 8 Character random ID's with lowercase, uppercase and numbers 0-9

# $maxID++;
srand();
my @alphanumeric = ('a'..'z', 'A'..'Z', 0..9);
my $randomID = join '', map $alphanumeric[rand @alphanumeric], 0..7;

$maxID=$randomID;

Offline Jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 1018
    • slipdex
Re: Random auto increment ID
« Reply #8 on: September 24, 2007, 09:18:15 AM »
Paul,

I have to say that your contributions, though they look like they are simple, are actually some of the BEST add ons and hacks for these scripts!!  I love this stuff.  I have a question about your random alphanumeric hack here, and I assume I'm right, but before testing, I wanted to check....

If I wanted say 20 digits instead of 8, I suppose I could change this line:
my $randomID = join '', map $alphanumeric[rand @alphanumeric], 0..7;

to:
my $randomID = join '', map $alphanumeric[rand @alphanumeric], 0..19;

Also, that quote mark: = join '', map : is that supposed to be there without a close quote?  I'm sure if I added it and tested I would figure it out, but I might slip this into an existing project and don't want too many hangups.

Jason
Founder of www.slipdex.com

Offline Paul

  • Hero Member
  • *****
  • Posts: 162
Re: Random auto increment ID
« Reply #9 on: September 24, 2007, 03:57:35 PM »
Quote
If I wanted say 20 digits instead of 8, I suppose I could change this line:
my $randomID = join '', map $alphanumeric[rand @alphanumeric], 0..7;

to:
my $randomID = join '', map $alphanumeric[rand @alphanumeric], 0..19;

Spot on Jason.


The double quote is actually two single quotes side by side ... I can't take the credit for writing it. I found the code here.
http://prefetch.net/blog/index.php/category/perl/

(Generating Random passwords with PERL)

Even with 8 character ID's, the chances of two being the same would be extremely remote
« Last Edit: September 24, 2007, 04:03:12 PM by Paul »