Selection based on ID number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sphincter66

    Selection based on ID number

    Hi

    I'm trying to write a function that will accept an ID number and
    depending on certain criteria (that's what I need help with) it will
    return a number between a specified range (lets say 1-10). The thing is
    it must return the same number every time so any random functions are
    out the window. I was using modulos but that's only good for 2 numbers.

    ID's could be any number at all but the most important thing is the
    number that is selected from the range must be the same for the ID
    every time the function is ran no matter if the range grows. That is if
    today I run it with a range of 1-10 and then tomorrow I run it with a
    range of 1-20 the number that was returned yesterday for ID=28, for
    example, should be the same number that is returned today for ID=28

    Hope I've manged to make that clear ;)

    Thanks

    S66

  • sphincter66

    #2
    Re: Selection based on ID number

    This is some pseudo code that might make it clearer.

    GetNumber(23)

    function GetNumber(ID){

    //this function will return a number within the range specified based
    on the ID passed

    //define range
    $min = 1;
    $max = 12;

    $retValue = 'do something with the id number so that we can return a
    value between the $min and $max values';

    return $retValue;

    }

    Thanks

    S66

    Comment

    • Ian.H

      #3
      Re: Selection based on ID number

      On 3 Apr 2005 06:38:37 -0700, "sphincter6 6" <sphincter66@ho tmail.com>
      wrote:
      [color=blue]
      >This is some pseudo code that might make it clearer.
      >
      >GetNumber(23 )
      >
      >function GetNumber(ID){
      >
      >//this function will return a number within the range specified based
      >on the ID passed
      >
      >//define range
      >$min = 1;
      >$max = 12;
      >
      >$retValue = 'do something with the id number so that we can return a
      >value between the $min and $max values';
      >
      >return $retValue;
      >
      >}
      >
      >Thanks
      >
      >S66[/color]



      You could store the first instance of the ID for later use.. something
      along the lines of:


      function getNumber($id) {
      return rand(1, 10);
      }


      $id = 23;
      $num = getNumber($id);


      $res = mysql_query("
      INSERT INTO " . SOME_TABLE . "
      (
      id,
      num
      )
      VALUES (
      $id,
      $num
      )
      ");

      [ do rest of SQL validation here ]



      Then next time, check to see if id 23 in the DB has a value assigned, if
      so, read and use that value, if not, call getNumber() to assign them an
      ID.


      I hope this is an example of what you're looking for =)



      Regards,

      Ian


      --
      Ian.H
      digiServ Network
      London, UK

      Comment

      • sphincter66

        #4
        Re: Selection based on ID number

        Thanks for the response Ian. I guess that would work too. I was kind of
        looking for a function that would actually be able to calculate the
        return value on the fly.

        If I can't come up with that solution this will be my 'fall back' plan.

        thanks again

        S66

        Comment

        • Ian.H

          #5
          Re: Selection based on ID number

          On 3 Apr 2005 06:58:30 -0700, "sphincter6 6" <sphincter66@ho tmail.com>
          wrote:

          Hi S..

          [color=blue]
          >Thanks for the response Ian. I guess that would work too. I was kind of
          >looking for a function that would actually be able to calculate the
          >return value on the fly.
          >
          >If I can't come up with that solution this will be my 'fall back' plan.
          >
          >thanks again
          >
          >S66[/color]


          No probs =)

          The "solution" I came up with was my initial thinking from the limited
          info I had. Could you explain a little more how this will all be used?
          Is it member id based based on certain criteria? or something completely
          different?

          You could write a function that just returns a fixed range of say 0-10,
          but I'm not seeing the relevence of feeding it an ID number if you want
          the output to be "static". I guess this is due to me not knowing how
          you're thinking of implementing it all =)

          If you need to return the same number.. I think you will end up having
          to use a "marker" of some description.. although there's bound to be
          many different ways to achieve this.



          Regards,

          Ian


          --
          Ian.H
          digiServ Network
          London, UK

          Comment

          • sphincter66

            #6
            Re: Selection based on ID number

            Hi Ian

            The relevance of feeding it the ID is that this is the unique
            identifier for the number returned. What I mean is that I need to have
            the same number returned from the range specified based on the ID
            passed. That is if 23 is passed as the ID and the range is 1-10 the
            return may be 3 for example. The next time the function is called I may
            have changed the range to 1-25 but if the ID passed is 23 it must still
            return 3.

            Your solution using a database would work but as I say I was really
            hoping to find a solution that would calculate the return value.

            Hope that clarifys it a bit.

            S66

            Comment

            • sphincter66

              #7
              Re: Selection based on ID number

              I've been thinking about this for a few hours and I'm not sure it can
              be done. I may have to go with Ians database solution.

              S66

              Comment

              • ZeldorBlat

                #8
                Re: Selection based on ID number

                I don't think so either, and here's the reason:

                You're taking an infinite set of inputs and mapping them to a finite
                set of outputs. So, suppose every number you could possibly input gets
                mapped to a number between 1 and 25. You send in every single number
                in the universe and get your mapping. The next day, you change your
                range to 1 - 30. But every number is already mapped to something
                between 1 and 25. So there can't be anything that would map to a
                number between 26 and 30, since all the mappings have already been
                created.

                Ian is correct in that you have to "remember" what got mapped to what
                (using a database or some other means).

                Comment

                • Ian.H

                  #9
                  Re: Selection based on ID number

                  On 3 Apr 2005 07:32:34 -0700, "sphincter6 6" <sphincter66@ho tmail.com>
                  wrote:

                  Hi S..
                  [color=blue]
                  >Hi Ian
                  >
                  >The relevance of feeding it the ID is that this is the unique
                  >identifier for the number returned. What I mean is that I need to have
                  >the same number returned from the range specified based on the ID
                  >passed. That is if 23 is passed as the ID and the range is 1-10 the
                  >return may be 3 for example. The next time the function is called I may
                  >have changed the range to 1-25 but if the ID passed is 23 it must still
                  >return 3.
                  >
                  >Your solution using a database would work but as I say I was really
                  >hoping to find a solution that would calculate the return value.
                  >
                  >Hope that clarifys it a bit.
                  >
                  >S66[/color]


                  Yup, it does.. but as you've since discovered, I don't think there is
                  going to be a way of doing this without storing a previous value if
                  assigned. There probably is, using some "extreme" code and lots of
                  maths, but none that I'm aware of without some major thought.

                  I know you've already pretty much had this said by Zeldor too.. but I
                  thought it only polite to at least reply =)

                  Hope whatever method you do finally use works out ok.



                  Regards,

                  Ian


                  --
                  Ian.H
                  digiServ Network
                  London, UK

                  Comment

                  Working...