Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 9th, 2007, 09:16 AM
Bron Bron is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Moscow, Russian Federation
Posts: 96 Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 1 h 24 sec
Reputation Power: 19
Classic ASP/VBScript - Random recorset when IDs are not consequtive

It`s very often that records are deleted from a table and IDs are not consequetive. I modified Memnochs' code slightly. It works, but might be not the best optimized, so gurus help in optimization would be very valuable

1. It does not care of frequency of display
2. It does not care about repetitions. Just pure randomizing.

Code:
'CODE TO GET RANDOM ID FROM DATABASE STARTS HERE
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open connectstr

' FIRST START: get random ID beween min & max IDs stored in database 

qry = "SELECT MAX(ID) As MaxID FROM Yourtablename"

Set oRS = oConn.Execute(qry)


If IsNull(oRS("MaxID")) Then
   MaxID = 1
Else
   MaxID = oRS("MaxID")
End If

LowNum = 1
HighNum = MaxID

oRS.close

Set oRs = nothing



' FIRST ENDS

' SECOND START: avoid missed IDs and define constant RandomIDfromDB


Do Until CheckRandomID<>""

Randomize()
RandomID = Int((HighNum - LowNum + 1)*Rnd() + LowNum)

qry = "SELECT ID FROM Yourtablename WHERE ID=" & RandomID & " "


Set oRS = oConn.Execute(qry)

if not oRS.EOF then
while not oRS.EOF


CheckRandomID = oRs.Fields("ID")

oRS.movenext
wend
oRS.close
end if

Set oRs = nothing

Loop

if CheckRandomID<>"" then
RandomIDfromDB=CheckRandomID
End If



' SECOND ENDS

Set oConn = nothing


'CODE TO GET RANDOM ID FROM DATABASE  ENDS HERE




Now we can use RandomIDfromDB to select random record.

Reply With Quote
  #2  
Old November 11th, 2007, 03:57 AM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
Click here for more information.
 
Join Date: Sep 2004
Location: Israel
Posts: 26,993 Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 8th Grade (Above 100000 Reputation Level)  Folding Points: 342508 Folding Title: Super Ultimate Folder - Level 1Folding Points: 342508 Folding Title: Super Ultimate Folder - Level 1Folding Points: 342508 Folding Title: Super Ultimate Folder - Level 1Folding Points: 342508 Folding Title: Super Ultimate Folder - Level 1Folding Points: 342508 Folding Title: Super Ultimate Folder - Level 1Folding Points: 342508 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 1 Week 5 Days 16 h 39 m 38 sec
Reputation Power: 1557
Is this code working properly? If not it should be here if yes then thanks for sharing.

Reply With Quote
  #3  
Old November 15th, 2007, 05:03 AM
Bron Bron is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Moscow, Russian Federation
Posts: 96 Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level)Bron User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 1 h 24 sec
Reputation Power: 19
Yes, it works on my website.

Yes, it works on my website. I use it to show random images, which are stored in DB as OLEobjects and called from DB by their IDs.
Comments on this post
Shadow Wizard agrees: nice, thanks for sharing

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Classic ASP/VBScript - Random recorset when IDs are not consequtive


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway