ill have a database with 1 table and 3 fields:

ID FIRSTNAME LASTNAME

(the ID field will be the auto incrementing index)

there might be 10 records in the DB, there might be 10,000.
i need to open the DB and randomly select a record (and then display the
name, which i dont have a problem with)
how can i randomly select a record? im guessing id have to open a recordset
and check the count to get the number of records, so lets say there were 100
records. i imagine i would have to generate a random number between 1 and
100....

anyone have a small example?

Re: how to choose a random record from a database by Aaron

Aaron
Fri Sep 08 09:56:00 CDT 2006

http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.html



"Jimmy" <j@j.j> wrote in message
news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl...
> ill have a database with 1 table and 3 fields:
>
> ID FIRSTNAME LASTNAME
>
> (the ID field will be the auto incrementing index)
>
> there might be 10 records in the DB, there might be 10,000.
> i need to open the DB and randomly select a record (and then display the
> name, which i dont have a problem with)
> how can i randomly select a record? im guessing id have to open a
> recordset and check the count to get the number of records, so lets say
> there were 100 records. i imagine i would have to generate a random number
> between 1 and 100....
>
> anyone have a small example?
>
>



Re: how to choose a random record from a database by Jimmy

Jimmy
Fri Sep 08 10:24:59 CDT 2006

This is a multi-part message in MIME format.

------=_NextPart_000_0012_01C6D339.6B9AEDF0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

thanks... few questions though. here is the code:

<%=20
Randomize()=20
randNum =3D (CInt(1000 * Rnd) + 1) * -1=20
=20
set conn =3D CreateObject("ADODB.Connection")=20
=20
sql =3D "SELECT TOP 1 cols," & _=20
"r =3D Rnd(" & randNum & ")" & _=20
"FROM TableName " & _=20
"ORDER BY r"=20
=20
set rs =3D conn.execute(sql)=20
=20
response.write rs(0)=20
=20
' ...=20
rs.close: set rs =3D nothing=20
conn.close: set conn =3D nothing=20
%>

what will randNum equal? a number between what and what?
in the SQL statement, what is "cols"? and "r"?



"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in =
message news:ODk1Cb10GHA.4452@TK2MSFTNGP02.phx.gbl...
> =
http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.ht=
ml
>=20
>=20
>=20
> "Jimmy" <j@j.j> wrote in message=20
> news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl...
>> ill have a database with 1 table and 3 fields:
>>
>> ID FIRSTNAME LASTNAME
>>
>> (the ID field will be the auto incrementing index)
>>
>> there might be 10 records in the DB, there might be 10,000.
>> i need to open the DB and randomly select a record (and then display =
the=20
>> name, which i dont have a problem with)
>> how can i randomly select a record? im guessing id have to open a=20
>> recordset and check the count to get the number of records, so lets =
say=20
>> there were 100 records. i imagine i would have to generate a random =
number=20
>> between 1 and 100....
>>
>> anyone have a small example?
>>
>>=20
>=20
>
------=_NextPart_000_0012_01C6D339.6B9AEDF0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2963" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>thanks... few questions though. here is =
the=20
code:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#0000ff =
size=3D1><STRONG>&lt;%&nbsp;<BR>&nbsp; &nbsp;=20
Randomize()&nbsp;<BR>&nbsp; &nbsp; randNum =3D (CInt(1000 * Rnd) + 1) *=20
-1&nbsp;<BR>&nbsp;<BR>&nbsp; &nbsp; set conn =3D=20
CreateObject("ADODB.Connection")&nbsp;<BR>&nbsp;<BR>&nbsp; &nbsp; sql =
=3D "SELECT=20
TOP 1 cols," &amp; _&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; "r =3D Rnd(" =
&amp;=20
randNum &amp; ")" &amp; _&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; "FROM =
TableName "=20
&amp; _&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; "ORDER BY=20
r"&nbsp;<BR>&nbsp;<BR>&nbsp; &nbsp; set rs =3D &nbsp; &nbsp;=20
conn.execute(sql)&nbsp;<BR>&nbsp;<BR>&nbsp; &nbsp; response.write=20
rs(0)&nbsp;<BR>&nbsp;<BR>&nbsp; &nbsp; ' ...&nbsp;<BR>&nbsp; &nbsp; =
rs.close:=20
set rs =3D nothing&nbsp;<BR>&nbsp; &nbsp; conn.close: set conn =3D=20
nothing&nbsp;<BR>%&gt;</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>what will randNum equal? a number =
between what and=20
what?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>in the SQL statement, what is "cols"? =
and=20
"r"?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>"Aaron Bertrand [SQL Server MVP]" =
&lt;</FONT><A=20
href=3D"mailto:ten.xoc@dnartreb.noraa"><FONT face=3DArial=20
size=3D2>ten.xoc@dnartreb.noraa</FONT></A><FONT face=3DArial =
size=3D2>&gt; wrote in=20
message </FONT><A =
href=3D"news:ODk1Cb10GHA.4452@TK2MSFTNGP02.phx.gbl"><FONT=20
face=3DArial =
size=3D2>news:ODk1Cb10GHA.4452@TK2MSFTNGP02.phx.gbl</FONT></A><FONT=20
face=3DArial size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt; =
</FONT><A=20
href=3D"http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-r=
ecord.html"><FONT=20
face=3DArial=20
size=3D2>http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-=
record.html</FONT></A><BR><FONT=20
face=3DArial size=3D2>&gt; <BR>&gt; <BR>&gt; <BR>&gt; "Jimmy" =
&lt;</FONT><A=20
href=3D"mailto:j@j.j"><FONT face=3DArial size=3D2>j@j.j</FONT></A><FONT =
face=3DArial=20
size=3D2>&gt; wrote in message <BR>&gt; </FONT><A=20
href=3D"news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl"><FONT face=3DArial=20
size=3D2>news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl</FONT></A><FONT =
face=3DArial=20
size=3D2>...<BR>&gt;&gt; ill have a database with 1 table and 3=20
fields:<BR>&gt;&gt;<BR>&gt;&gt; ID&nbsp;&nbsp; FIRSTNAME&nbsp;&nbsp;=20
LASTNAME<BR>&gt;&gt;<BR>&gt;&gt; (the ID field will be the auto =
incrementing=20
index)<BR>&gt;&gt;<BR>&gt;&gt; there might be 10 records in the DB, =
there might=20
be 10,000.<BR>&gt;&gt; i need to open the DB and randomly select a =
record (and=20
then display the <BR>&gt;&gt; name, which i dont have a problem=20
with)<BR>&gt;&gt; how can i randomly select a record? im guessing id =
have to=20
open a <BR>&gt;&gt; recordset and check the count to get the number of =
records,=20
so lets say <BR>&gt;&gt; there were 100 records. i imagine i would have =
to=20
generate a random number <BR>&gt;&gt; between 1 and=20
100....<BR>&gt;&gt;<BR>&gt;&gt; anyone have a small=20
example?<BR>&gt;&gt;<BR>&gt;&gt; <BR>&gt; <BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_0012_01C6D339.6B9AEDF0--


Re: how to choose a random record from a database by Aaron

Aaron
Fri Sep 08 11:14:04 CDT 2006

> what will randNum equal? a number between what and what?

Hit refresh a few times:

<%
Randomize()
randNum = (CInt(1000 * Rnd) + 1) * -1
response.write (randNum)
%>

> In the SQL statement, what is "cols"? and "r"?

cols is your column list.

r is just an alias for the random number, and it should be in the result set
because it use to seed the random number in the query and is used in the
order by. You could do it all in the order by but some queries will not
allow you to order by something that is not in the select list.




"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:ODk1Cb10GHA.4452@TK2MSFTNGP02.phx.gbl...
> http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.html
>
>
>
> "Jimmy" <j@j.j> wrote in message
> news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl...
>> ill have a database with 1 table and 3 fields:
>>
>> ID FIRSTNAME LASTNAME
>>
>> (the ID field will be the auto incrementing index)
>>
>> there might be 10 records in the DB, there might be 10,000.
>> i need to open the DB and randomly select a record (and then display the
>> name, which i dont have a problem with)
>> how can i randomly select a record? im guessing id have to open a
>> recordset and check the count to get the number of records, so lets say
>> there were 100 records. i imagine i would have to generate a random
>> number
>> between 1 and 100....
>>
>> anyone have a small example?
>>
>>
>
>



Re: how to choose a random record from a database by Jimmy

Jimmy
Fri Sep 08 11:55:26 CDT 2006

ok so

randNum = (CInt(9 * Rnd) + 1)

gives me 1-10, correct?
so to generate a random record from my database i would use:

randNum = (CInt((recordcount-1) * Rnd) + 1)


is that right?



"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:uKXlqG20GHA.4188@TK2MSFTNGP04.phx.gbl...
>> what will randNum equal? a number between what and what?
>
> Hit refresh a few times:
>
> <%
> Randomize()
> randNum = (CInt(1000 * Rnd) + 1) * -1
> response.write (randNum)
> %>
>
>> In the SQL statement, what is "cols"? and "r"?
>
> cols is your column list.
>
> r is just an alias for the random number, and it should be in the result
> set because it use to seed the random number in the query and is used in
> the order by. You could do it all in the order by but some queries will
> not allow you to order by something that is not in the select list.
>
>
>
>
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
> message news:ODk1Cb10GHA.4452@TK2MSFTNGP02.phx.gbl...
>> http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.html
>>
>>
>>
>> "Jimmy" <j@j.j> wrote in message
>> news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl...
>>> ill have a database with 1 table and 3 fields:
>>>
>>> ID FIRSTNAME LASTNAME
>>>
>>> (the ID field will be the auto incrementing index)
>>>
>>> there might be 10 records in the DB, there might be 10,000.
>>> i need to open the DB and randomly select a record (and then display the
>>> name, which i dont have a problem with)
>>> how can i randomly select a record? im guessing id have to open a
>>> recordset and check the count to get the number of records, so lets say
>>> there were 100 records. i imagine i would have to generate a random
>>> number
>>> between 1 and 100....
>>>
>>> anyone have a small example?
>>>
>>>
>>
>>
>
>



Re: how to choose a random record from a database by Aaron

Aaron
Fri Sep 08 12:49:57 CDT 2006

Well, you have the same ability to try that code as I do. It really depends
on what you are then going to do with RandNum. What I was trying to
demonstrate is that you don't need to know the recordcount beforehand
(saving one query).


"Jimmy" <j@j.j> wrote in message
news:O8VlUe20GHA.4452@TK2MSFTNGP02.phx.gbl...
> ok so
>
> randNum = (CInt(9 * Rnd) + 1)
>
> gives me 1-10, correct?
> so to generate a random record from my database i would use:
>
> randNum = (CInt((recordcount-1) * Rnd) + 1)
>
>
> is that right?
>
>
>
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
> message news:uKXlqG20GHA.4188@TK2MSFTNGP04.phx.gbl...
>>> what will randNum equal? a number between what and what?
>>
>> Hit refresh a few times:
>>
>> <%
>> Randomize()
>> randNum = (CInt(1000 * Rnd) + 1) * -1
>> response.write (randNum)
>> %>
>>
>>> In the SQL statement, what is "cols"? and "r"?
>>
>> cols is your column list.
>>
>> r is just an alias for the random number, and it should be in the result
>> set because it use to seed the random number in the query and is used in
>> the order by. You could do it all in the order by but some queries will
>> not allow you to order by something that is not in the select list.
>>
>>
>>
>>
>> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
>> message news:ODk1Cb10GHA.4452@TK2MSFTNGP02.phx.gbl...
>>> http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.html
>>>
>>>
>>>
>>> "Jimmy" <j@j.j> wrote in message
>>> news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl...
>>>> ill have a database with 1 table and 3 fields:
>>>>
>>>> ID FIRSTNAME LASTNAME
>>>>
>>>> (the ID field will be the auto incrementing index)
>>>>
>>>> there might be 10 records in the DB, there might be 10,000.
>>>> i need to open the DB and randomly select a record (and then display
>>>> the
>>>> name, which i dont have a problem with)
>>>> how can i randomly select a record? im guessing id have to open a
>>>> recordset and check the count to get the number of records, so lets say
>>>> there were 100 records. i imagine i would have to generate a random
>>>> number
>>>> between 1 and 100....
>>>>
>>>> anyone have a small example?
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Re: how to choose a random record from a database by Jimmy

Jimmy
Fri Sep 08 12:56:56 CDT 2006

if i dont know the recordhand, how will i seed the random number generator?

obviously if i have 100 records i cant have a number generated thats over
100



"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:uq56P820GHA.4108@TK2MSFTNGP04.phx.gbl...
> Well, you have the same ability to try that code as I do. It really
> depends on what you are then going to do with RandNum. What I was trying
> to demonstrate is that you don't need to know the recordcount beforehand
> (saving one query).
>
>
> "Jimmy" <j@j.j> wrote in message
> news:O8VlUe20GHA.4452@TK2MSFTNGP02.phx.gbl...
>> ok so
>>
>> randNum = (CInt(9 * Rnd) + 1)
>>
>> gives me 1-10, correct?
>> so to generate a random record from my database i would use:
>>
>> randNum = (CInt((recordcount-1) * Rnd) + 1)
>>
>>
>> is that right?
>>
>>
>>
>> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
>> message news:uKXlqG20GHA.4188@TK2MSFTNGP04.phx.gbl...
>>>> what will randNum equal? a number between what and what?
>>>
>>> Hit refresh a few times:
>>>
>>> <%
>>> Randomize()
>>> randNum = (CInt(1000 * Rnd) + 1) * -1
>>> response.write (randNum)
>>> %>
>>>
>>>> In the SQL statement, what is "cols"? and "r"?
>>>
>>> cols is your column list.
>>>
>>> r is just an alias for the random number, and it should be in the result
>>> set because it use to seed the random number in the query and is used in
>>> the order by. You could do it all in the order by but some queries will
>>> not allow you to order by something that is not in the select list.
>>>
>>>
>>>
>>>
>>> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
>>> message news:ODk1Cb10GHA.4452@TK2MSFTNGP02.phx.gbl...
>>>> http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.html
>>>>
>>>>
>>>>
>>>> "Jimmy" <j@j.j> wrote in message
>>>> news:OreY4Y10GHA.4108@TK2MSFTNGP04.phx.gbl...
>>>>> ill have a database with 1 table and 3 fields:
>>>>>
>>>>> ID FIRSTNAME LASTNAME
>>>>>
>>>>> (the ID field will be the auto incrementing index)
>>>>>
>>>>> there might be 10 records in the DB, there might be 10,000.
>>>>> i need to open the DB and randomly select a record (and then display
>>>>> the
>>>>> name, which i dont have a problem with)
>>>>> how can i randomly select a record? im guessing id have to open a
>>>>> recordset and check the count to get the number of records, so lets
>>>>> say
>>>>> there were 100 records. i imagine i would have to generate a random
>>>>> number
>>>>> between 1 and 100....
>>>>>
>>>>> anyone have a small example?
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Re: how to choose a random record from a database by Aaron

Aaron
Fri Sep 08 13:02:19 CDT 2006

Did you TRY the code sample that you had questions about? The random number
is not important, it is merely used to seed the random number in the query
that gets *1* row.

I suggest you try it out.




"Jimmy" <j@j.j> wrote in message
news:e46crA30GHA.5048@TK2MSFTNGP05.phx.gbl...
> if i dont know the recordhand, how will i seed the random number
> generator?
>
> obviously if i have 100 records i cant have a number generated thats over
> 100



Re: how to choose a random record from a database by Jimmy

Jimmy
Fri Sep 08 13:04:33 CDT 2006

i dont have a database ready yet to TRY it. so instead im trying to
UNDERSTAND it first.
my confusion is in the fact that im thinking if i have 100 records, i need
to generate a random number between 1 and 100 so that i can open THAT random
record. do you see what im trying to do?



"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:uq69JD30GHA.4656@TK2MSFTNGP04.phx.gbl...
> Did you TRY the code sample that you had questions about? The random
> number is not important, it is merely used to seed the random number in
> the query that gets *1* row.
>
> I suggest you try it out.
>
>
>
>
> "Jimmy" <j@j.j> wrote in message
> news:e46crA30GHA.5048@TK2MSFTNGP05.phx.gbl...
>> if i dont know the recordhand, how will i seed the random number
>> generator?
>>
>> obviously if i have 100 records i cant have a number generated thats over
>> 100
>
>



Re: how to choose a random record from a database by Aaron

Aaron
Fri Sep 08 13:18:00 CDT 2006

>i dont have a database ready yet to TRY it. so instead im trying to
>UNDERSTAND it first.
> my confusion is in the fact that im thinking if i have 100 records, i need
> to generate a random number between 1 and 100 so that i can open THAT
> random record. do you see what im trying to do?

Sort of.

The problem is, if you have a number between 1 and 100, and you are trying
to get the row where [TableName]ID = that number, you're going to be
disappointed when you don't have a perfectly sequential set of [TableName]ID
values (even if the total number is exactly 100). Because of deletes,
rollbacks, and failures, you are likely going to have gaps in your
[TableName]ID. This is why the top solution is better than a solution that
relies on mapping recordcount to actual data.

A



Re: how to choose a random record from a database by Bob

Bob
Fri Sep 08 13:53:50 CDT 2006

Does either the pubs or Northwind sample database come with SQLExpress?
If so, apply the example query to a table in one of those databases.

Let's look at the query:
sql = "SELECT TOP 1

TOP 1 tells it to return only the first record in the resultset

cols," & _

cols is meant to be a list of the columns you wish the query to return

"r = Rnd(" & randNum & ")" & _

This assigns a random number to each record in the resultset (prior to
TOP being applied)

"FROM TableName " & _
"ORDER BY r"

Without the "TOP 1", you would have a resultset containing the columns
specified by "cols" as well as a calculated column (called r) containing
a random number generated by the Rnd function, ordered by the random
number assigned to each record. The "TOP 1" returns the first one. So
you don't need to know how many records are in your table.

Jimmy wrote:
> i dont have a database ready yet to TRY it. so instead im trying to
> UNDERSTAND it first.
> my confusion is in the fact that im thinking if i have 100 records, i
> need to generate a random number between 1 and 100 so that i can open
> THAT random record. do you see what im trying to do?
>
>
>
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
> message news:uq69JD30GHA.4656@TK2MSFTNGP04.phx.gbl...
>> Did you TRY the code sample that you had questions about? The random
>> number is not important, it is merely used to seed the random number
>> in the query that gets *1* row.
>>
>> I suggest you try it out.
>>
>>
>>
>>
>> "Jimmy" <j@j.j> wrote in message
>> news:e46crA30GHA.5048@TK2MSFTNGP05.phx.gbl...
>>> if i dont know the recordhand, how will i seed the random number
>>> generator?
>>>
>>> obviously if i have 100 records i cant have a number generated
>>> thats over 100

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: how to choose a random record from a database by Jimmy

Jimmy
Fri Sep 08 13:58:17 CDT 2006

This is a multi-part message in MIME format.

------=_NextPart_000_0007_01C6D357.375C2900
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

this is what i have working so far... tell me what you think:

<%
Dim oConn, oRS, randNum

Set oConn=3DServer.CreateObject("ADODB.Connection")
Set oRS=3DServer.CreateObject("ADODB.recordset")

oConn.Provider=3D"Microsoft.Jet.OLEDB.4.0"
oConn.Open Server.MapPath("temp.mdb")

oRS.Open "SELECT EMAIL_ADDRESS FROM TABLE1", oConn, adOpenStatic, =
adLockReadOnly
Randomize()
randNum =3D CInt((oRS.RecordCount - 1) * Rnd)

Response.Write("RecordCount: " & oRS.RecordCount & "<br><br>")

oRS.Move randNum
Response.Write oRS("EMAIL_ADDRESS")

oRS.close
oConn.close
Set oConn =3D nothing
Set oRS =3D nothing
%>

this opens up the table, gets a count of num records, generates a random =
number between 0 and numrecords-1, then moves to that record, and =
displays a random email address.

seems like it works perfectly... do you see any issues?



"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in =
message news:ODU96L30GHA.1568@TK2MSFTNGP03.phx.gbl...
> >i dont have a database ready yet to TRY it. so instead im trying to=20
>>UNDERSTAND it first.
>> my confusion is in the fact that im thinking if i have 100 records, i =
need=20
>> to generate a random number between 1 and 100 so that i can open THAT =

>> random record. do you see what im trying to do?
>=20
> Sort of.
>=20
> The problem is, if you have a number between 1 and 100, and you are =
trying=20
> to get the row where [TableName]ID =3D that number, you're going to be =

> disappointed when you don't have a perfectly sequential set of =
[TableName]ID=20
> values (even if the total number is exactly 100). Because of deletes, =

> rollbacks, and failures, you are likely going to have gaps in your=20
> [TableName]ID. This is why the top solution is better than a solution =
that=20
> relies on mapping recordcount to actual data.
>=20
> A=20
>=20
>
------=_NextPart_000_0007_01C6D357.375C2900
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2963" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>this is what i have working so far... =
tell me what=20
you think:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial =
color=3D#0000ff=20
size=3D1><STRONG>&lt;%<BR>Dim oConn, oRS, =
randNum</STRONG></FONT></DIV><FONT=20
face=3DArial color=3D#0000ff size=3D1><STRONG>
<DIV><BR>Set oConn=3DServer.CreateObject("ADODB.Connection")<BR>Set=20
oRS=3DServer.CreateObject("ADODB.recordset")</DIV>
<DIV><BR>oConn.Provider=3D"Microsoft.Jet.OLEDB.4.0"<BR>oConn.Open=20
Server.MapPath("temp.mdb")</STRONG></FONT></DIV>
<DIV><STRONG><FONT color=3D#0000ff size=3D1></FONT></STRONG>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D1><STRONG>oRS.Open =
"SELECT=20
EMAIL_ADDRESS FROM TABLE1", oConn, adOpenStatic,=20
adLockReadOnly<BR>Randomize()<BR>randNum =3D CInt((oRS.RecordCount - 1) =
*=20
Rnd)</STRONG></FONT></DIV>
<DIV><FONT face=3DArial color=3D#0000ff=20
size=3D1><STRONG><BR>Response.Write("RecordCount: " &amp; =
oRS.RecordCount &amp;=20
"&lt;br&gt;&lt;br&gt;")</STRONG></FONT></DIV>
<DIV><STRONG><FONT color=3D#0000ff size=3D1></FONT></STRONG>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#0000ff size=3D1><STRONG>oRS.Move=20
randNum<BR>Response.Write oRS("EMAIL_ADDRESS")</STRONG></FONT></DIV>
<DIV><STRONG><FONT color=3D#0000ff size=3D1></FONT></STRONG>&nbsp;</DIV>
<DIV><FONT face=3DArial color=3D#0000ff=20
size=3D1><STRONG>oRS.close<BR>oConn.close<BR>Set oConn =3D =
nothing<BR>Set oRS =3D=20
nothing<BR>%&gt;</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>this opens up the table, gets a count =
of num=20
records, generates a random number between 0 and numrecords-1, then =
moves to=20
that record, and displays a random email address.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>seems like it works perfectly... do you =
see any=20
issues?</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>"Aaron Bertrand [SQL Server MVP]" =
&lt;</FONT><A=20
href=3D"mailto:ten.xoc@dnartreb.noraa"><FONT face=3DArial=20
size=3D2>ten.xoc@dnartreb.noraa</FONT></A><FONT face=3DArial =
size=3D2>&gt; wrote in=20
message </FONT><A =
href=3D"news:ODU96L30GHA.1568@TK2MSFTNGP03.phx.gbl"><FONT=20
face=3DArial =
size=3D2>news:ODU96L30GHA.1568@TK2MSFTNGP03.phx.gbl</FONT></A><FONT=20
face=3DArial size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt; =
&gt;i dont have a=20
database ready yet to TRY it. so instead im trying to =
<BR>&gt;&gt;UNDERSTAND it=20
first.<BR>&gt;&gt; my confusion is in the fact that im thinking if i =
have 100=20
records, i need <BR>&gt;&gt; to generate a random number between 1 and =
100 so=20
that i can open THAT <BR>&gt;&gt; random record. do you see what im =
trying to=20
do?<BR>&gt; <BR>&gt; Sort of.<BR>&gt; <BR>&gt; The problem is, if you =
have a=20
number between 1 and 100, and you are trying <BR>&gt; to get the row =
where=20
[TableName]ID =3D that number, you're going to be <BR>&gt; disappointed =
when you=20
don't have a perfectly sequential set of [TableName]ID <BR>&gt; values =
(even if=20
the total number is exactly 100).&nbsp; Because of deletes, <BR>&gt; =
rollbacks,=20
and failures, you are likely going to have gaps in your <BR>&gt;=20
[TableName]ID.&nbsp; This is why the top solution is better than a =
solution that=20
<BR>&gt; relies on mapping recordcount to actual data.<BR>&gt; <BR>&gt; =
A=20
<BR>&gt; <BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_0007_01C6D357.375C2900--


Re: how to choose a random record from a database by Bob

Bob
Fri Sep 08 14:02:01 CDT 2006

Jimmy wrote:
> this is what i have working so far... tell me what you think:
> oRS.Open "SELECT EMAIL_ADDRESS FROM TABLE1", oConn, adOpenStatic,
> adLockReadOnly
> Randomize()
> randNum = CInt((oRS.RecordCount - 1) * Rnd)
>
> Response.Write("RecordCount: " & oRS.RecordCount & "<br><br>")
>
> oRS.Move randNum

>
> seems like it works perfectly... do you see any issues?
>

Yes. You're retrieving all the records from the database when you only
need one of them. Not a very efficient use of network or server
resources.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: how to choose a random record from a database by Jimmy

Jimmy
Fri Sep 08 14:05:11 CDT 2006

thank you.
im currently testing with an access db. do you see any issues with my
previous random record generating code?


"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
news:%23wJHhg30GHA.4748@TK2MSFTNGP04.phx.gbl...
> Does either the pubs or Northwind sample database come with SQLExpress?
> If so, apply the example query to a table in one of those databases.
>
> Let's look at the query:
> sql = "SELECT TOP 1
>
> TOP 1 tells it to return only the first record in the resultset
>
> cols," & _
>
> cols is meant to be a list of the columns you wish the query to return
>
> "r = Rnd(" & randNum & ")" & _
>
> This assigns a random number to each record in the resultset (prior to
> TOP being applied)
>
> "FROM TableName " & _
> "ORDER BY r"
>
> Without the "TOP 1", you would have a resultset containing the columns
> specified by "cols" as well as a calculated column (called r) containing
> a random number generated by the Rnd function, ordered by the random
> number assigned to each record. The "TOP 1" returns the first one. So
> you don't need to know how many records are in your table.
>
> Jimmy wrote:
>> i dont have a database ready yet to TRY it. so instead im trying to
>> UNDERSTAND it first.
>> my confusion is in the fact that im thinking if i have 100 records, i
>> need to generate a random number between 1 and 100 so that i can open
>> THAT random record. do you see what im trying to do?
>>
>>
>>
>> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
>> message news:uq69JD30GHA.4656@TK2MSFTNGP04.phx.gbl...
>>> Did you TRY the code sample that you had questions about? The random
>>> number is not important, it is merely used to seed the random number
>>> in the query that gets *1* row.
>>>
>>> I suggest you try it out.
>>>
>>>
>>>
>>>
>>> "Jimmy" <j@j.j> wrote in message
>>> news:e46crA30GHA.5048@TK2MSFTNGP05.phx.gbl...
>>