Choosing a random entry in a table by "weight"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alpha.beta0@googlemail.com

    Choosing a random entry in a table by "weight"

    I have a MySQL table of servers, I use RAND() to pick a random server
    to use each time, but how can I add a number to each server entry that
    allows it be to picked more often than the other 20 servers?

    For example Server1's weight is 80 and Server2's weight is 40 and
    hence Server1 is more likely to be picked than the others.

  • Floortje

    #2
    Re: Choosing a random entry in a table by "weight&qu ot;

    alpha.beta0@goo glemail.com wrote:
    I have a MySQL table of servers, I use RAND() to pick a random server
    to use each time, but how can I add a number to each server entry that
    allows it be to picked more often than the other 20 servers?
    >
    For example Server1's weight is 80 and Server2's weight is 40 and
    hence Server1 is more likely to be picked than the others.
    >
    I dont think myqsl can do that (corect me if im wrong please)

    If the weights aren't too large i would solve it something like this:

    Offcourse this will get verry ineffective if a=1,b=100000000 , etc

    link->weight
    a->1
    b->3
    c->2

    ->select all links
    ->create a new array $w=(a,b,b,b,c,c )

    and voilla
    $link=$w[rand(1,count($w ))];

    --
    Arjen
    http://www.hondenpage.com - mijn site over honden



    Comment

    • Floortje

      #3
      Re: Choosing a random entry in a table by "weight&qu ot;

      Floortje wrote:

      Whoops
      $link=$w[rand(0,(count($ w)-1))];


      Comment

      • Rami Elomaa

        #4
        Re: Choosing a random entry in a table by "weight&qu ot;

        alpha.beta0@goo glemail.com kirjoitti:
        I have a MySQL table of servers, I use RAND() to pick a random server
        to use each time, but how can I add a number to each server entry that
        allows it be to picked more often than the other 20 servers?
        >
        For example Server1's weight is 80 and Server2's weight is 40 and
        hence Server1 is more likely to be picked than the others.
        >
        In theory like this: pick a random weight and take into group all the
        items that weigh more, it's more likely that heavier items are taken
        into the group, than the lighter. From the group you have created, pick
        one randomly. If no items were in the group, your random weight was too
        big, so you might limit it to < maxweight, so that always at least one
        item is picked.

        Transforming this into a query is another thing. This is very rough
        example but you'll get the idea, I suppose:

        SELECT * FROM ( SELECT * FROM servers WHERE weight RAND() ) ORDER BY
        RAND() LIMIT 1,1

        --
        Rami.Elomaa@gma il.com

        "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
        usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

        Comment

        • petersprc

          #5
          Re: Choosing a random entry in a table by &quot;weight&qu ot;

          On May 27, 12:10 pm, alpha.be...@goo glemail.com wrote:
          I have a MySQL table of servers, I use RAND() to pick a random server
          to use each time, but how can I add a number to each server entry that
          allows it be to picked more often than the other 20 servers?
          >
          For example Server1's weight is 80 and Server2's weight is 40 and
          hence Server1 is more likely to be picked than the others.
          You could also do something like:

          select serverId from server order by rand() / weight limit 1

          Comment

          • Floortje

            #6
            Re: Choosing a random entry in a table by &quot;weight&qu ot;

            Rami Elomaa wrote:
            alpha.beta0@goo glemail.com kirjoitti:
            >I have a MySQL table of servers, I use RAND() to pick a random server
            >to use each time, but how can I add a number to each server entry that
            >allows it be to picked more often than the other 20 servers?
            >>
            >For example Server1's weight is 80 and Server2's weight is 40 and
            >hence Server1 is more likely to be picked than the others.
            >>
            >
            In theory like this: pick a random weight and take into group all the
            items that weigh more, it's more likely that heavier items are taken
            into the group, than the lighter. From the group you have created, pick
            one randomly. If no items were in the group, your random weight was too
            big, so you might limit it to < maxweight, so that always at least one
            item is picked.
            >
            Transforming this into a query is another thing. This is very rough
            example but you'll get the idea, I suppose:
            >
            SELECT * FROM ( SELECT * FROM servers WHERE weight RAND() ) ORDER BY
            RAND() LIMIT 1,1
            Nice .. real nice but wrong :-(

            say you have this
            a-2
            b-1
            c-1
            d-1

            Valid random weight numbers are 1 and 2 (both 50%)

            Then your chances of picking A are 62.5% while B,C,D only get a 12.5%
            change of being picked (instead of 40-20-20-20)

            --
            Arjen
            HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

            Comment

            • Rami Elomaa

              #7
              Re: Choosing a random entry in a table by &quot;weight&qu ot;

              Floortje kirjoitti:
              Rami Elomaa wrote:
              >alpha.beta0@goo glemail.com kirjoitti:
              >>I have a MySQL table of servers, I use RAND() to pick a random server
              >>to use each time, but how can I add a number to each server entry that
              >>allows it be to picked more often than the other 20 servers?
              >>>
              >>For example Server1's weight is 80 and Server2's weight is 40 and
              >>hence Server1 is more likely to be picked than the others.
              >>>
              >>
              >In theory like this: pick a random weight and take into group all the
              >items that weigh more, it's more likely that heavier items are taken
              >into the group, than the lighter. From the group you have created,
              >pick one randomly. If no items were in the group, your random weight
              >was too big, so you might limit it to < maxweight, so that always at
              >least one item is picked.
              >>
              >Transforming this into a query is another thing. This is very rough
              >example but you'll get the idea, I suppose:
              >>
              >SELECT * FROM ( SELECT * FROM servers WHERE weight RAND() ) ORDER BY
              >RAND() LIMIT 1,1
              >
              Nice .. real nice but wrong :-(
              I don't know what would make it particularily "wrong", it does basicly
              what it should, but the distribution of percentages is different. How
              would I know what is "wrong" and what "right", so I just say it's
              "different" . The original question does not define exactly what is "more
              often than the others".

              --
              Rami.Elomaa@gma il.com

              "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
              usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

              Comment

              • Floortje

                #8
                Re: Choosing a random entry in a table by &quot;weight&qu ot;

                Rami Elomaa schreef:
                >Nice .. real nice but wrong :-(
                >
                I don't know what would make it particularily "wrong", it does basicly
                what it should, but the distribution of percentages is different. How
                would I know what is "wrong" and what "right", so I just say it's
                "different" . The original question does not define exactly what is "more
                often than the others".

                The op asked for 'weighted'. I really like your solution but it is not
                weighted. http://en.wikipedia.org/wiki/Weight_function


                --
                Arjen
                HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

                Comment

                • Floortje

                  #9
                  Re: Choosing a random entry in a table by &quot;weight&qu ot;

                  >Nice .. real nice but wrong :-(
                  >
                  I don't know what would make it particularily "wrong", it does basicly
                  what it should, but the distribution of percentages is different. How
                  would I know what is "wrong" and what "right", so I just say it's
                  "different" . The original question does not define exactly what is "more
                  often than the others".
                  The op asked for 'weighted'. I really like your solution but it is not
                  weighted. http://en.wikipedia.org/wiki/Weight_function



                  --
                  Arjen
                  HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

                  Comment

                  • Alexey Kulentsov

                    #10
                    Re: Choosing a random entry in a table by &quot;weight&qu ot;

                    alpha.beta0@goo glemail.com wrote:
                    I have a MySQL table of servers, I use RAND() to pick a random server
                    to use each time, but how can I add a number to each server entry that
                    allows it be to picked more often than the other 20 servers?
                    >
                    For example Server1's weight is 80 and Server2's weight is 40 and
                    hence Server1 is more likely to be picked than the others.
                    >
                    SELECT *, weight*RAND() as result FROM Servers ORDER BY result LIMIT 1

                    Comment

                    • Alexey Kulentsov

                      #11
                      Re: Choosing a random entry in a table by &quot;weight&qu ot;

                      Alexey Kulentsov wrote:
                      SELECT *, weight*RAND() as result FROM Servers ORDER BY result LIMIT 1
                      .... ORDER BY result DESC ...

                      Comment

                      • alpha.beta0@googlemail.com

                        #12
                        Re: Choosing a random entry in a table by &quot;weight&qu ot;

                        On May 29, 12:55 pm, Alexey Kulentsov <criman...@crim aniak.comwrote:
                        SELECT *, weight*RAND() as result FROM Servers ORDER BY result LIMIT 1
                        SELECT *, Server_Weight*R AND() as result FROM Servers ORDER BY result
                        DESC LIMIT 1

                        Thanks, that works nice.

                        Comment

                        Working...