Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / October 2005



Tip: Looking for answers? Try searching our database.

Weighted Random Record????

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
johnegbert - 28 Oct 2005 15:35 GMT
Guys,
Good morning.... I have a random script I am using to pull a random row....  

<cfset displayRow = randrange(1,randompic.recordCount)>

<cfoutput>#randompic.mlocation[displayRow]#</cfoutput>

.....
However, I want to make this a weighted formula.  In other words, one of the
records in my query has a better chance of appearing than another.  Any ideas?  
Kronin555 - 28 Oct 2005 16:29 GMT
Depending on where else that database table is used, and what constraints you
have on the table, the easiest thing to do would be to just duplicate the row
you want weighted more.

Whether this is the best method will depend on how else that table is used,
etc.
johnegbert - 28 Oct 2005 17:06 GMT
Kronin555
thanks man for takin a shot, thats not exactly what I am trying to do...  I
already have a field in the table called weight....  I am going to set it so
that it can be like an integer, 1, 2, 3 ,4 ,5 .... depending on how much weight
i want it to have...  so like 1 would be less likely to get picked, but 5 would
be much more likely.. i know there has to be some sort of multiplication
algorithm, but I cant figure it out.......
Someone help me with this?  Anyone else have ideas?
Mr Black - 28 Oct 2005 22:18 GMT
Same thing, as it was already desribed by Kronin.  You have to physycally or
logically add duplicate records according to their weights.

1. Find total sum of weights in the recordset.  Good idea to remember array of
running totals (will be needed).
2. Generate random number between 1 and the total.
3. First record that has running weight total greater or equal to the
generated number is the one.

Example:

rec   weight  total
=============
1         1          1
2         2          3
3         2          5
4         3          8  <------  6
5         1          9

random number (1,9) is 6 (well, not always):

1 2 2 3 3 4 4 4 5  record # is 4
                 ^
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.