473,544 Members | 263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to choose a random record from a database

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?
Sep 8 '06 #1
26 3121
http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or******** ******@TK2MSFTN GP04.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?


Sep 8 '06 #2
thanks... few questions though. here is the code:

<%
Randomize()
randNum = (CInt(1000 * Rnd) + 1) * -1

set conn = CreateObject("A DODB.Connection ")

sql = "SELECT TOP 1 cols," & _
"r = Rnd(" & randNum & ")" & _
"FROM TableName " & _
"ORDER BY r"

set rs = conn.execute(sq l)

response.write rs(0)

' ...
rs.close: set rs = nothing
conn.close: set conn = nothing
%>

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

"Aaron Bertrand [SQL Server MVP]" <te*****@dnartr eb.noraawrote in message news:OD******** ******@TK2MSFTN GP02.phx.gbl...
http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or******** ******@TK2MSFTN GP04.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?

Sep 8 '06 #3
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]" <te*****@dnartr eb.noraawrote in message
news:OD******** ******@TK2MSFTN GP02.phx.gbl...
http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or******** ******@TK2MSFTN GP04.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?



Sep 8 '06 #4
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((recordco unt-1) * Rnd) + 1)
is that right?

"Aaron Bertrand [SQL Server MVP]" <te*****@dnartr eb.noraawrote in message
news:uK******** ******@TK2MSFTN GP04.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]" <te*****@dnartr eb.noraawrote in
message news:OD******** ******@TK2MSFTN GP02.phx.gbl...
>http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or******* *******@TK2MSFT NGP04.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?




Sep 8 '06 #5
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.jwrote in message
news:O8******** ******@TK2MSFTN GP02.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((recordco unt-1) * Rnd) + 1)
is that right?

"Aaron Bertrand [SQL Server MVP]" <te*****@dnartr eb.noraawrote in
message news:uK******** ******@TK2MSFTN GP04.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]" <te*****@dnartr eb.noraawrote in
message news:OD******** ******@TK2MSFTN GP02.phx.gbl...
>>http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or****** ********@TK2MSF TNGP04.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?




Sep 8 '06 #6
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]" <te*****@dnartr eb.noraawrote in message
news:uq******** ******@TK2MSFTN GP04.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.jwrote in message
news:O8******** ******@TK2MSFTN GP02.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((recordco unt-1) * Rnd) + 1)
is that right?

"Aaron Bertrand [SQL Server MVP]" <te*****@dnartr eb.noraawrote in
message news:uK******** ******@TK2MSFTN GP04.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]" <te*****@dnartr eb.noraawrote in
message news:OD******** ******@TK2MSFTN GP02.phx.gbl...
http://databases.aspfaq.com/database...om-record.html

"Jimmy" <j@j.jwrote in message
news:Or***** *********@TK2MS FTNGP04.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?
>
>




Sep 8 '06 #7
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]" <te*****@dnartr eb.noraawrote in message
news:uq******** ******@TK2MSFTN GP04.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.jwrote in message
news:e4******** ******@TK2MSFTN GP05.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


Sep 8 '06 #8
>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
Sep 8 '06 #9
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]" <te*****@dnartr eb.noraawrote in
message news:uq******** ******@TK2MSFTN GP04.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.jwrote in message
news:e4******* *******@TK2MSFT NGP05.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.
Sep 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6277
by: Keith Griffiths | last post by:
I'm trying to do a search under a set criteria followed by a selection of random entries meeting this criteria. But I don't seem to be able to achieve this. The idea being to search on say subject and then select a random set of records meeting that subject. Any ideas or thought would be helpful. I'm using Access XP to try this out. ...
7
6913
by: Bill | last post by:
Hello, I am trying to use a SQL Query to return a random record from an Access 2000 Database. I am using: SELECT TOP 1 Example FROM TABLE ORDER BY Rnd;
4
5151
by: yf | last post by:
A KB article "http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells that the maximum number of records that a table may hold if the PRIMARY key data type is set to AUTONUMBER is 4,294,967,295. Suppose the PRIMARY key data type is set to "RANDOM" AutoNumber. Suppose an application (a) successfully INSERTS "X" records, then...
2
2505
by: sugaray | last post by:
I want to write a school computer billing system, one of the function is to distribute machine id using rand() for each student log on, suppose there's 100 machines, when each person log on, the system will store user's info such as name, student id number, log on time, log out time into a database file, when the next student log on, the...
21
3755
by: Gary Bond | last post by:
Hi All, I am a bit stuck with a project: Specifically, when making a database like engine in 'the old days', I would have wrapped a record class with a stream class, so I could have a file of records on disc, such that I could always jump straight to the record number I wanted. Simple random file access. Maybe they were fixed length...
48
4198
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) * -1 Set oConn=Server.CreateObject("ADODB.Connection") Set oRS=Server.CreateObject("ADODB.recordset") oConn.Provider="Microsoft.Jet.OLEDB.4.0"...
3
3871
by: John Fairhurst | last post by:
Hi, The following code should select the specified number of records randomly from the database <% .... query = "SELECT FROM " Set RS = Server.CreateObject("ADODB.Recordset")
15
2712
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to pick from a list a random element that satisfies a given property. All or none of the elements may have the property. Most of the time, many of the...
0
7424
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7772
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7376
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7709
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5297
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4918
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
661
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.