Unix Technical Forum

Research Subjects drawn randomly from databases

This is a discussion on Research Subjects drawn randomly from databases within the MySQL General forum forums, part of the MySQL category; --> For possible educational research purposes, I was playing around with a query that would randomly select people from a ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:16 AM
John Kebbel
 
Posts: n/a
Default Research Subjects drawn randomly from databases

For possible educational research purposes, I was playing around with a
query that would randomly select people from a database. The database I
experiment with has a group of fictitious persons with id numbers
(primary key) ranging sequentially from 2 to 378. When I ran these
queries below, I was expecting to select five random persons from the
database. The query partially worked. I was getting random subjects, but
everytime I ran the query, I got a different number of subjects,
stretching from 0 and up (sometimes as many as 8 or 9). I could see the
query generating fewer rows if I duplicated an id or made an off-by-one
error, but I don't see how it could generate more than five. Does anyone
see my error? (I've used two equivalent forms for the query below; both
did the same thing)

select id,first,middle,last from persons where id = ceil(rand()*377+1)
or id = ceil(rand()*377+1) or id = ceil(rand()*377+1) or id =
ceil(rand()*377+1) or id = ceil(rand()*377+1);

select id,first,middle,last from persons where id in (ceil(rand()*377
+1), ceil(rand()*377+1), ceil(rand()*377+1), ceil(rand()*377+1),
ceil(rand()*377+1));
+------+-------------+--------+----------+
| id | first | middle | last |
+------+-------------+--------+----------+
| 35 | Viridiana | W | McCarthy |
| 47 | Crystal | O | Cassady |
| 67 | Ricardo | L | Johnson |
| 183 | Christopher | E | Denver |
| 237 | Christopher | B | Brenner |
| 255 | Danielle | W | Nickels |
| 299 | Christine | D | Dexter |
| 300 | Rachel | J | Baker |
| 339 | Jenna | O | Murray |
+------+-------------+--------+----------+
9 rows in set (0.00 sec)


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:17 AM
Michael Dykman
 
Posts: n/a
Default Re: Research Subjects drawn randomly from databases

If might suggest:

SELECT * FROM BAR
ORDER BY RAND()
LIMIT 10

On 4/29/07, John Kebbel <kebbelj@scripting-solutions.com> wrote:
> For possible educational research purposes, I was playing around with a
> query that would randomly select people from a database. The database I
> experiment with has a group of fictitious persons with id numbers
> (primary key) ranging sequentially from 2 to 378. When I ran these
> queries below, I was expecting to select five random persons from the
> database. The query partially worked. I was getting random subjects, but
> everytime I ran the query, I got a different number of subjects,
> stretching from 0 and up (sometimes as many as 8 or 9). I could see the
> query generating fewer rows if I duplicated an id or made an off-by-one
> error, but I don't see how it could generate more than five. Does anyone
> see my error? (I've used two equivalent forms for the query below; both
> did the same thing)
>
> select id,first,middle,last from persons where id = ceil(rand()*377+1)
> or id = ceil(rand()*377+1) or id = ceil(rand()*377+1) or id =
> ceil(rand()*377+1) or id = ceil(rand()*377+1);
>
> select id,first,middle,last from persons where id in (ceil(rand()*377
> +1), ceil(rand()*377+1), ceil(rand()*377+1), ceil(rand()*377+1),
> ceil(rand()*377+1));
> +------+-------------+--------+----------+
> | id | first | middle | last |
> +------+-------------+--------+----------+
> | 35 | Viridiana | W | McCarthy |
> | 47 | Crystal | O | Cassady |
> | 67 | Ricardo | L | Johnson |
> | 183 | Christopher | E | Denver |
> | 237 | Christopher | B | Brenner |
> | 255 | Danielle | W | Nickels |
> | 299 | Christine | D | Dexter |
> | 300 | Rachel | J | Baker |
> | 339 | Jenna | O | Murray |
> +------+-------------+--------+----------+
> 9 rows in set (0.00 sec)
>
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
>
>



--
- michael dykman
- mdykman@gmail.com

- All models are wrong. Some models are useful.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:17 AM
John Kebbel
 
Posts: n/a
Default Re: Research Subjects drawn randomly from databases

I rewrote my line using your suggestion ...

select id,first,middle,last from persons order by rand() limit 10;

and it worked perfectly. I'm still curious about why my original version
gave such cockeyed results, but I'll focus on the successful solution
and leave that unsolved problem for another day. Thank you for your
solution Michael.

On Sun, 2007-04-29 at 08:48 -0400, Michael Dykman wrote:
> If might suggest:
>
> SELECT * FROM BAR
> ORDER BY RAND()
> LIMIT 10
>
> On 4/29/07, John Kebbel <kebbelj@scripting-solutions.com> wrote:
> > For possible educational research purposes, I was playing around with a
> > query that would randomly select people from a database. The database I
> > experiment with has a group of fictitious persons with id numbers
> > (primary key) ranging sequentially from 2 to 378. When I ran these
> > queries below, I was expecting to select five random persons from the
> > database. The query partially worked. I was getting random subjects, but
> > everytime I ran the query, I got a different number of subjects,
> > stretching from 0 and up (sometimes as many as 8 or 9). I could see the
> > query generating fewer rows if I duplicated an id or made an off-by-one
> > error, but I don't see how it could generate more than five. Does anyone
> > see my error? (I've used two equivalent forms for the query below; both
> > did the same thing)
> >
> > select id,first,middle,last from persons where id = ceil(rand()*377+1)
> > or id = ceil(rand()*377+1) or id = ceil(rand()*377+1) or id =
> > ceil(rand()*377+1) or id = ceil(rand()*377+1);
> >
> > select id,first,middle,last from persons where id in (ceil(rand()*377
> > +1), ceil(rand()*377+1), ceil(rand()*377+1), ceil(rand()*377+1),
> > ceil(rand()*377+1));
> > +------+-------------+--------+----------+
> > | id | first | middle | last |
> > +------+-------------+--------+----------+
> > | 35 | Viridiana | W | McCarthy |
> > | 47 | Crystal | O | Cassady |
> > | 67 | Ricardo | L | Johnson |
> > | 183 | Christopher | E | Denver |
> > | 237 | Christopher | B | Brenner |
> > | 255 | Danielle | W | Nickels |
> > | 299 | Christine | D | Dexter |
> > | 300 | Rachel | J | Baker |
> > | 339 | Jenna | O | Murray |
> > +------+-------------+--------+----------+
> > 9 rows in set (0.00 sec)
> >
> >
> >
> > --
> > MySQL General Mailing List
> > For list archives: http://lists.mysql.com/mysql
> > To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
> >
> >

>
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:17 AM
Jerry Schwartz
 
Posts: n/a
Default RE: Research Subjects drawn randomly from databases

Is your ID field an integer? If not, you might be running into some rounding
corner cases. I don't see why that would happen, off-hand, since integers
can be stored exactly as binary floating point numbers, but who knows.

To satisfy your curiosity, you could

SELECT COUNT(*) FROM table WHERE ROUND(id) != id;

If you get a non-zero count, then you know that there is a possibility of
CEIL(RND()) not hitting an ID.

Regards,

Jerry Schwartz
Global Information Incorporated
195 Farmington Ave.
Farmington, CT 06032

860.674.8796 / FAX: 860.674.8341


> -----Original Message-----
> From: John Kebbel [mailto:kebbelj@scripting-solutions.com]
> Sent: Sunday, April 29, 2007 11:49 AM
> To: MySQL
> Subject: Re: Research Subjects drawn randomly from databases
>
> I rewrote my line using your suggestion ...
>
> select id,first,middle,last from persons order by rand() limit 10;
>
> and it worked perfectly. I'm still curious about why my
> original version
> gave such cockeyed results, but I'll focus on the successful solution
> and leave that unsolved problem for another day. Thank you for your
> solution Michael.
>
> On Sun, 2007-04-29 at 08:48 -0400, Michael Dykman wrote:
> > If might suggest:
> >
> > SELECT * FROM BAR
> > ORDER BY RAND()
> > LIMIT 10
> >
> > On 4/29/07, John Kebbel <kebbelj@scripting-solutions.com> wrote:
> > > For possible educational research purposes, I was playing

> around with a
> > > query that would randomly select people from a database.

> The database I
> > > experiment with has a group of fictitious persons with id numbers
> > > (primary key) ranging sequentially from 2 to 378. When I ran these
> > > queries below, I was expecting to select five random

> persons from the
> > > database. The query partially worked. I was getting

> random subjects, but
> > > everytime I ran the query, I got a different number of subjects,
> > > stretching from 0 and up (sometimes as many as 8 or 9). I

> could see the
> > > query generating fewer rows if I duplicated an id or made

> an off-by-one
> > > error, but I don't see how it could generate more than

> five. Does anyone
> > > see my error? (I've used two equivalent forms for the

> query below; both
> > > did the same thing)
> > >
> > > select id,first,middle,last from persons where id =

> ceil(rand()*377+1)
> > > or id = ceil(rand()*377+1) or id = ceil(rand()*377+1) or id =
> > > ceil(rand()*377+1) or id = ceil(rand()*377+1);
> > >
> > > select id,first,middle,last from persons where id in

> (ceil(rand()*377
> > > +1), ceil(rand()*377+1), ceil(rand()*377+1), ceil(rand()*377+1),
> > > ceil(rand()*377+1));
> > > +------+-------------+--------+----------+
> > > | id | first | middle | last |
> > > +------+-------------+--------+----------+
> > > | 35 | Viridiana | W | McCarthy |
> > > | 47 | Crystal | O | Cassady |
> > > | 67 | Ricardo | L | Johnson |
> > > | 183 | Christopher | E | Denver |
> > > | 237 | Christopher | B | Brenner |
> > > | 255 | Danielle | W | Nickels |
> > > | 299 | Christine | D | Dexter |
> > > | 300 | Rachel | J | Baker |
> > > | 339 | Jenna | O | Murray |
> > > +------+-------------+--------+----------+
> > > 9 rows in set (0.00 sec)
> > >
> > >
> > >
> > > --
> > > MySQL General Mailing List
> > > For list archives: http://lists.mysql.com/mysql
> > > To unsubscribe:

> http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
> > >
> > >

> >
> >

>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
>
>




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 06:17 AM
John Kebbel
 
Posts: n/a
Default RE: Research Subjects drawn randomly from databases

id is an integer ...

describe persons;
+------------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra
|
+------------+---------------------------+------+-----+---------+-------+
| ID | int(11) | YES | | NULL |

I got a 0 count ...

SELECT COUNT(*) FROM persons WHERE ROUND(id) != id;
+----------+
| COUNT(*) |
+----------+
| 0 |
+----------+
1 row in set (0.06 sec)


On Mon, 2007-04-30 at 10:45 -0400, Jerry Schwartz wrote:
> Is your ID field an integer? If not, you might be running into some rounding
> corner cases. I don't see why that would happen, off-hand, since integers
> can be stored exactly as binary floating point numbers, but who knows.
>
> To satisfy your curiosity, you could
>
> SELECT COUNT(*) FROM table WHERE ROUND(id) != id;
>
> If you get a non-zero count, then you know that there is a possibility of
> CEIL(RND()) not hitting an ID.
>
> Regards,
>
> Jerry Schwartz
> Global Information Incorporated
> 195 Farmington Ave.
> Farmington, CT 06032
>
> 860.674.8796 / FAX: 860.674.8341
>
>
> > -----Original Message-----
> > From: John Kebbel [mailto:kebbelj@scripting-solutions.com]
> > Sent: Sunday, April 29, 2007 11:49 AM
> > To: MySQL
> > Subject: Re: Research Subjects drawn randomly from databases
> >
> > I rewrote my line using your suggestion ...
> >
> > select id,first,middle,last from persons order by rand() limit 10;
> >
> > and it worked perfectly. I'm still curious about why my
> > original version
> > gave such cockeyed results, but I'll focus on the successful solution
> > and leave that unsolved problem for another day. Thank you for your
> > solution Michael.
> >
> > On Sun, 2007-04-29 at 08:48 -0400, Michael Dykman wrote:
> > > If might suggest:
> > >
> > > SELECT * FROM BAR
> > > ORDER BY RAND()
> > > LIMIT 10
> > >
> > > On 4/29/07, John Kebbel <kebbelj@scripting-solutions.com> wrote:
> > > > For possible educational research purposes, I was playing

> > around with a
> > > > query that would randomly select people from a database.

> > The database I
> > > > experiment with has a group of fictitious persons with id numbers
> > > > (primary key) ranging sequentially from 2 to 378. When I ran these
> > > > queries below, I was expecting to select five random

> > persons from the
> > > > database. The query partially worked. I was getting

> > random subjects, but
> > > > everytime I ran the query, I got a different number of subjects,
> > > > stretching from 0 and up (sometimes as many as 8 or 9). I

> > could see the
> > > > query generating fewer rows if I duplicated an id or made

> > an off-by-one
> > > > error, but I don't see how it could generate more than

> > five. Does anyone
> > > > see my error? (I've used two equivalent forms for the

> > query below; both
> > > > did the same thing)
> > > >
> > > > select id,first,middle,last from persons where id =

> > ceil(rand()*377+1)
> > > > or id = ceil(rand()*377+1) or id = ceil(rand()*377+1) or id =
> > > > ceil(rand()*377+1) or id = ceil(rand()*377+1);
> > > >
> > > > select id,first,middle,last from persons where id in

> > (ceil(rand()*377
> > > > +1), ceil(rand()*377+1), ceil(rand()*377+1), ceil(rand()*377+1),
> > > > ceil(rand()*377+1));
> > > > +------+-------------+--------+----------+
> > > > | id | first | middle | last |
> > > > +------+-------------+--------+----------+
> > > > | 35 | Viridiana | W | McCarthy |
> > > > | 47 | Crystal | O | Cassady |
> > > > | 67 | Ricardo | L | Johnson |
> > > > | 183 | Christopher | E | Denver |
> > > > | 237 | Christopher | B | Brenner |
> > > > | 255 | Danielle | W | Nickels |
> > > > | 299 | Christine | D | Dexter |
> > > > | 300 | Rachel | J | Baker |
> > > > | 339 | Jenna | O | Murray |
> > > > +------+-------------+--------+----------+
> > > > 9 rows in set (0.00 sec)
> > > >
> > > >
> > > >
> > > > --
> > > > MySQL General Mailing List
> > > > For list archives: http://lists.mysql.com/mysql
> > > > To unsubscribe:

> > http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
> > > >
> > > >
> > >
> > >

> >
> >
> > --
> > MySQL General Mailing List
> > For list archives: http://lists.mysql.com/mysql
> > To unsubscribe:
> > http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
> >
> >

>
>
>
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 06:17 AM
Jerry Schwartz
 
Posts: n/a
Default RE: Research Subjects drawn randomly from databases

Well, then color me baffled. I don't know why your first query didn't work.

Regards,

Jerry Schwartz
Global Information Incorporated
195 Farmington Ave.
Farmington, CT 06032

860.674.8796 / FAX: 860.674.8341


> -----Original Message-----
> From: John Kebbel [mailto:kebbelj@scripting-solutions.com]
> Sent: Monday, April 30, 2007 2:42 PM
> To: MySQL
> Subject: RE: Research Subjects drawn randomly from databases
>
> id is an integer ...
>
> describe persons;
> +------------+---------------------------+------+-----+-------
> --+-------+
> | Field | Type | Null | Key |
> Default | Extra
> |
> +------------+---------------------------+------+-----+-------
> --+-------+
> | ID | int(11) | YES | | NULL
> |
>
> I got a 0 count ...
>
> SELECT COUNT(*) FROM persons WHERE ROUND(id) != id;
> +----------+
> | COUNT(*) |
> +----------+
> | 0 |
> +----------+
> 1 row in set (0.06 sec)
>
>
> On Mon, 2007-04-30 at 10:45 -0400, Jerry Schwartz wrote:
> > Is your ID field an integer? If not, you might be running

> into some rounding
> > corner cases. I don't see why that would happen, off-hand,

> since integers
> > can be stored exactly as binary floating point numbers, but

> who knows.
> >
> > To satisfy your curiosity, you could
> >
> > SELECT COUNT(*) FROM table WHERE ROUND(id) != id;
> >
> > If you get a non-zero count, then you know that there is a

> possibility of
> > CEIL(RND()) not hitting an ID.
> >
> > Regards,
> >
> > Jerry Schwartz
> > Global Information Incorporated
> > 195 Farmington Ave.
> > Farmington, CT 06032
> >
> > 860.674.8796 / FAX: 860.674.8341
> >
> >
> > > -----Original Message-----
> > > From: John Kebbel [mailto:kebbelj@scripting-solutions.com]
> > > Sent: Sunday, April 29, 2007 11:49 AM
> > > To: MySQL
> > > Subject: Re: Research Subjects drawn randomly from databases
> > >
> > > I rewrote my line using your suggestion ...
> > >
> > > select id,first,middle,last from persons order by rand()

> limit 10;
> > >
> > > and it worked perfectly. I'm still curious about why my
> > > original version
> > > gave such cockeyed results, but I'll focus on the

> successful solution
> > > and leave that unsolved problem for another day. Thank

> you for your
> > > solution Michael.
> > >
> > > On Sun, 2007-04-29 at 08:48 -0400, Michael Dykman wrote:
> > > > If might suggest:
> > > >
> > > > SELECT * FROM BAR
> > > > ORDER BY RAND()
> > > > LIMIT 10
> > > >
> > > > On 4/29/07, John Kebbel <kebbelj@scripting-solutions.com> wrote:
> > > > > For possible educational research purposes, I was playing
> > > around with a
> > > > > query that would randomly select people from a database.
> > > The database I
> > > > > experiment with has a group of fictitious persons

> with id numbers
> > > > > (primary key) ranging sequentially from 2 to 378.

> When I ran these
> > > > > queries below, I was expecting to select five random
> > > persons from the
> > > > > database. The query partially worked. I was getting
> > > random subjects, but
> > > > > everytime I ran the query, I got a different number

> of subjects,
> > > > > stretching from 0 and up (sometimes as many as 8 or 9). I
> > > could see the
> > > > > query generating fewer rows if I duplicated an id or made
> > > an off-by-one
> > > > > error, but I don't see how it could generate more than
> > > five. Does anyone
> > > > > see my error? (I've used two equivalent forms for the
> > > query below; both
> > > > > did the same thing)
> > > > >
> > > > > select id,first,middle,last from persons where id =
> > > ceil(rand()*377+1)
> > > > > or id = ceil(rand()*377+1) or id = ceil(rand()*377+1) or id =
> > > > > ceil(rand()*377+1) or id = ceil(rand()*377+1);
> > > > >
> > > > > select id,first,middle,last from persons where id in
> > > (ceil(rand()*377
> > > > > +1), ceil(rand()*377+1), ceil(rand()*377+1),

> ceil(rand()*377+1),
> > > > > ceil(rand()*377+1));
> > > > > +------+-------------+--------+----------+
> > > > > | id | first | middle | last |
> > > > > +------+-------------+--------+----------+
> > > > > | 35 | Viridiana | W | McCarthy |
> > > > > | 47 | Crystal | O | Cassady |
> > > > > | 67 | Ricardo | L | Johnson |
> > > > > | 183 | Christopher | E | Denver |
> > > > > | 237 | Christopher | B | Brenner |
> > > > > | 255 | Danielle | W | Nickels |
> > > > > | 299 | Christine | D | Dexter |
> > > > > | 300 | Rachel | J | Baker |
> > > > > | 339 | Jenna | O | Murray |
> > > > > +------+-------------+--------+----------+
> > > > > 9 rows in set (0.00 sec)
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > MySQL General Mailing List
> > > > > For list archives: http://lists.mysql.com/mysql
> > > > > To unsubscribe:
> > > http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> > > --
> > > MySQL General Mailing List
> > > For list archives: http://lists.mysql.com/mysql
> > > To unsubscribe:
> > > http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
> > >
> > >

> >
> >
> >
> >

>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
>
>




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:25 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com