Professional Web Applications Themes

Weighted Random Record???? - Coldfusion - Advanced Techniques

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?...

  1. #1

    Default Weighted Random Record????

    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?

    johnegbert Guest

  2. #2

    Default Re: Weighted Random Record????

    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.

    Kronin555 Guest

  3. #3

    Default Re: Weighted Random Record????

    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?

    johnegbert Guest

  4. #4

    Default Re: Weighted Random Record????

    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
    ^


    Mr Guest

Similar Threads

  1. Random record
    By barbedwire103 in forum Dreamweaver AppDev
    Replies: 4
    Last Post: June 13th, 12:29 PM
  2. Choose a random record
    By J?J in forum Macromedia ColdFusion
    Replies: 0
    Last Post: March 27th, 07:55 PM
  3. random record from small set
    By Jeff in forum PostgreSQL / PGSQL
    Replies: 5
    Last Post: February 15th, 04:15 PM
  4. GoTo Random Record script??
    By Joseph Witkin in forum FileMaker
    Replies: 5
    Last Post: October 19th, 09:59 PM
  5. How to Pick a Random Record?
    By Jack in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: July 22nd, 04:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139