Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: select random records from the recordset


Message #1 by "Chirag Shah" <chiragiit@y...> on Wed, 23 Oct 2002 16:58:13
My SQL Query returns recordset with Record No: 5,9,13,7.. (Record no. is 
primary key) it query against the database, I want to randomly select only 
one Record No and display it to the User.

say for example query returns record no. 5,9,13,6,

first time display record no: 6
if user refresh thier page display  another record say record no 13


Any easy way to do it.

My idea is after querying put resulted records in an array and randomly 
pick one record from that array. I would appreciate If someone can post 
few lines of sample code..


  
Message #2 by "Peter Foti (PeterF)" <PeterF@S...> on Wed, 23 Oct 2002 12:10:46 -0400
' To produce random integers in a given range, use this formula:
' Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
' upperbound is the highest number in the range, and 
' lowerbound is the lowest number in the range.

So, if you've got your recordset into an array (myArray) of n items (where n
is the number of items in the array), then the the upperbound will be n - 1,
and the lowerbound will be 0 (since array indexes start at zero).

<%
' Create your array and call it myArray
Randomize

Dim randomIndex
Dim upperbound
Dim lowerbound

upperbound = UBound(myArray)
lowerbound = 0
randomIndex = Int( ( upperbound - lowerbound + 1 ) * Rnd + lowerbound )

' Now use randomIndex as the index to your array
' myArray(randomIndex)
%>


Hope this helps.
Peter


-----Original Message-----
From: Chirag Shah [mailto:chiragiit@y...]
Sent: Wednesday, October 23, 2002 4:58 PM
To: ASP Databases
Subject: [asp_databases] select random records from the recordset


My SQL Query returns recordset with Record No: 5,9,13,7.. 
(Record no. is 
primary key) it query against the database, I want to randomly 
select only 
one Record No and display it to the User.

say for example query returns record no. 5,9,13,6,

first time display record no: 6
if user refresh thier page display  another record say record no 13


Any easy way to do it.

My idea is after querying put resulted records in an array and randomly 
pick one record from that array. I would appreciate If someone can post 
few lines of sample code..


  
Message #3 by "Chirag Shah" <chiragiit@y...> on Wed, 23 Oct 2002 17:18:17
---------------------------------------------------------
Actually, my question should be

How do you randomly select from  some fixed set of numbers like 
1,52,9,86,4,6,98

randomly select only one from (1,52,9,86,4,6,98) each time user refresh 
their page 
----------------------------------------------------------

 
 
Message #4 by "Peter Foti (PeterF)" <PeterF@S...> on Wed, 23 Oct 2002 12:18:24 -0400
You could put just those numbers into an array, and then use the code that I
posted to randomly access that array.  So if randomIndex was equal to 3, and
your array looked like this:

myArray(0) = 1
myArray(1) = 52
myArray(2) = 9
myArray(3) = 86
myArray(4) = 4
myArray(5) = 6
myArray(6) = 98

then myArray(randomIndex) would reference the value 86.

Regards,
Peter


-----Original Message-----
From: Chirag Shah [mailto:chiragiit@y...]
Sent: Wednesday, October 23, 2002 5:18 PM
To: ASP Databases
Subject: [asp_databases] Re: select random records from the recordset


---------------------------------------------------------
Actually, my question should be

How do you randomly select from  some fixed set of numbers like 
1,52,9,86,4,6,98

randomly select only one from (1,52,9,86,4,6,98) each time user refresh 
their page 
----------------------------------------------------------

 
 
Message #5 by "Ken Schaefer" <ken@a...> on Thu, 24 Oct 2002 11:24:02 +1000
What database are you using?

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Chirag Shah" <chiragiit@y...>
Subject: [asp_databases] select random records from the recordset


: My SQL Query returns recordset with Record No: 5,9,13,7.. (Record no. is
: primary key) it query against the database, I want to randomly select only
: one Record No and display it to the User.
:
: say for example query returns record no. 5,9,13,6,
:
: first time display record no: 6
: if user refresh thier page display  another record say record no 13
:
:
: Any easy way to do it.
:
: My idea is after querying put resulted records in an array and randomly
: pick one record from that array. I would appreciate If someone can post
: few lines of sample code..
:
:
:

Message #6 by "Chirag Shah" <chiragiit@y...> on Thu, 24 Oct 2002 13:37:31
MS Access 2000

> What database are you using?

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Chirag Shah" <chiragiit@y...>
Subject: [asp_databases] select random records from the recordset


: My SQL Query returns recordset with Record No: 5,9,13,7.. (Record no. is
: primary key) it query against the database, I want to randomly select 
only
: one Record No and display it to the User.
:
: say for example query returns record no. 5,9,13,6,
:
: first time display record no: 6
: if user refresh thier page display  another record say record no 13
:
:
: Any easy way to do it.
:
: My idea is after querying put resulted records in an array and randomly
: pick one record from that array. I would appreciate If someone can post
: few lines of sample code..
:
:
:


  Return to Index