SQlite's random() is a pseudo-random (as are most all) so there is no collision 
until you get the same value back at which point it just repeats the whole 
sequence again.

So the following example should work fine for him.  When it collides you've 
cycled through the complete range of SQlite's random() function.  I'm not sure 
what the repeat cycle is but I'm sure it's quite large.

create table t(a integer primary key, b int);
insert into t values(random(),1);

The rowid will match the random() value inserted as should last_rowid.



Michael D. Black
Senior Scientist
NG Information Systems
Advanced Analytics Directorate



________________________________________
From: [email protected] [[email protected]] on 
behalf of Drake Wilson [[email protected]]
Sent: Friday, March 04, 2011 3:21 PM
To: General Discussion of SQLite Database
Subject: EXT :Re: [sqlite] Adjusting strategy for ROWIDs

Quoth Enrico Thierbach <[email protected]>, on 2011-03-04 22:11:07 +0100:
> If I insert a record into a table with a primary key column id,
> Sqlite assigns a ROWID as outlined here
> http://www.sqlite.org/autoinc.html. However, I would like to assign
> a totally random rowid.

Why do you want to do this?  In particular, why would it not work to
randomize the values from the application side during inserts?

Random numbers collide faster than you might expect if you're not
familiar with the birthday paradox; normally, truly-random values that
are expected to have no collisions are generated using entropy from
the environment and are at least 128 bits long.  I suspect this is not
what you want.  If you just want them to be "random-looking" then it
may be more convenient to relate the underlying ID and the exterior ID
through a suitable permutation of the 64-bit integer space.  If
neither of those is true, you're probably looking at probing several
times to avoid collisions, and that's not something the stock "pick a
new row ID" mechanism handles AFAIK.

   ---> Drake Wilson
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to