Need help to custom the rand function ,

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

    #1

    Need help to custom the rand function ,

    Hi guys,

    I was wondering how could i get a random number exemple between 1 and
    100 but with n% of getting some value over a number x.

    Thanks a lot in advance.

    Fred
  • Jerry Stuckle

    #2
    Re: Need help to custom the rand function ,

    Fred wrote:
    Hi guys,
    >
    I was wondering how could i get a random number exemple between 1 and
    100 but with n% of getting some value over a number x.
    >
    Thanks a lot in advance.
    >
    Fred
    >
    If you want n% chance of getting a value over x, then it isn't a random
    number.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Fred

      #3
      Re: Need help to custom the rand function ,

      On 31 jan, 14:05, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      Fred wrote:
      Hi guys,
      >
      I was wondering how could i get a random number exemple between 1 and
      100 but with n% of getting some value over a number x.
      >
      Thanks a lot in advance.
      >
      Fred
      >
      If you want n% chance of getting a value over x, then it isn't a random
      number.
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===
      Wow man that help me a lot =)

      Seriously can you point me somewhere that could help ?

      Thanks

      Comment

      • The Natural Philosopher

        #4
        Re: Need help to custom the rand function ,

        Fred wrote:
        Hi guys,
        >
        I was wondering how could i get a random number exemple between 1 and
        100 but with n% of getting some value over a number x.
        >
        Thanks a lot in advance.
        >
        Fred
        Never mind Jerry, No one else does.


        So you want a random number from a weighted distribution?


        Your spec doesn't contain quite enough data to formulate the formula,
        since there are an infinite number of distributions that have N% over
        value X..

        One example, is that you first of all get a random number between 0 and
        100: If this is greater than N, then select a random number greater than
        X, else select one less than X.

        So in pseudo code where RAND(X) returns a number between 0 and X-1...


        if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(1 00-X));



        Comment

        • Fred

          #5
          Re: Need help to custom the rand function ,

          On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrot e:
          Fred wrote:
          Hi guys,
          >
          I was wondering how could i get a random number exemple between 1 and
          100 but with n% of getting some value over a number x.
          >
          Thanks a lot in advance.
          >
          Fred
          >
          Never mind Jerry, No one else does.
          >
          So you want a random number from a weighted distribution?
          >
          Your spec doesn't contain quite enough data to formulate the formula,
          since there are an infinite number of distributions that have N% over
          value X..
          >
          One example, is that you first of all get a random number between 0 and
          100: If this is greater than N, then select a random number greater than
          X, else select one less than X.
          >
          So in pseudo code where RAND(X) returns a number between 0 and X-1...
          >
          if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(1 00-X));
          Thanks for the info, i wont mind on him then =)

          Almost there with the code,

          Let me add some real number in the exemple :

          i know before the random what number i must have over let take 50 for
          the exemple,
          i also need the random to still pick between 0 and 100, but with a
          chance of 60% (exemple) to draw something over 50 .

          Thanks

          Comment

          • Jerry Stuckle

            #6
            Re: Need help to custom the rand function ,

            Fred wrote:
            On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrot e:
            >Fred wrote:
            >>Hi guys,
            >>I was wondering how could i get a random number exemple between 1 and
            >>100 but with n% of getting some value over a number x.
            >>Thanks a lot in advance.
            >>Fred
            >Never mind Jerry, No one else does.
            >>
            >So you want a random number from a weighted distribution?
            >>
            >Your spec doesn't contain quite enough data to formulate the formula,
            >since there are an infinite number of distributions that have N% over
            >value X..
            >>
            >One example, is that you first of all get a random number between 0 and
            >100: If this is greater than N, then select a random number greater than
            >X, else select one less than X.
            >>
            >So in pseudo code where RAND(X) returns a number between 0 and X-1...
            >>
            >if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(1 00-X));
            >
            Thanks for the info, i wont mind on him then =)
            >
            Almost there with the code,
            >
            Let me add some real number in the exemple :
            >
            i know before the random what number i must have over let take 50 for
            the exemple,
            i also need the random to still pick between 0 and 100, but with a
            chance of 60% (exemple) to draw something over 50 .
            >
            Thanks
            >
            Actually, the troll "Natural Philosopher" is one one who should be
            ignored. He seldom has anything to say that is correct. I was just
            pointing out you really aren't looking for a random number, from your
            description. Maybe a minor point to you, but very important when
            creating an algorithm.

            The real question here is, what kind of distribution do you want? For
            example, do you want a linear distribution, an exponential or
            logarithmic distribution, or like you said here, a 60% chance of being
            over 50?

            In the case you cited, you want 1-50 to occur 40% of the time, and
            51-100 to occur 60% of the time (approximately) . Something like this
            might work (not tested but the algorithm should work):

            function notReallyRand() (
            $temp = mt_rand(0,999);
            if ($temp < 400)
            return intval($temp/8);
            else
            return intval((($temp-400) / 12) + 50);
            }

            It gets a random number between 0 and 999. If the random number is
            between 0 and 399 (inclusive), it returns value / 8 as an int (0-49).

            If the value is >= 400, it subtracts 400 from the value (0-599) and
            divides by 12 (0-49.xx). It add 50 to this and returns the int value
            (50-99).

            You can adjust values as necessary.




            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Betikci Boris

              #7
              Re: Need help to custom the rand function ,

              On Jan 31, 8:56 pm, Fred <himse...@gmail .comwrote:
              Hi guys,
              >
              I was wondering how could i get a random number exemple between 1 and
              100 but with n% of getting some value over a number x.
              >
              Thanks a lot in advance.
              >
              Fred
              Here is the code ;)

              for($i=1;$i<=10 0;$i++){
              $rnum=rand(1,10 0);
              if($rnum>x)
              $bigx++;
              else $smallx++;
              $count++;
              }
              $ratio=$bigx/$count;
              echo "Ratio of the created random numbers bigger than x is :" .
              $ratio;



              Comment

              • C. (http://symcbean.blogspot.com/)

                #8
                Re: Need help to custom the rand function ,

                On 31 Jan, 19:29, The Natural Philosopher <a...@b.cwrot e:
                Fred wrote:
                Hi guys,
                >
                I was wondering how could i get a random number exemple between 1 and
                100 but with n% of getting some value over a number x.
                >
                Thanks a lot in advance.
                >
                Fred
                >
                Never mind Jerry, No one else does.
                >
                So you want a random number from a weighted distribution?
                >
                Your spec doesn't contain quite enough data to formulate the formula,
                since there are an infinite number of distributions that have N% over
                value X..
                >
                One example, is that you first of all get a random number between 0 and
                100: If this is greater than N, then select a random number greater than
                X, else select one less than X.
                >
                So in pseudo code where RAND(X) returns a number between 0 and X-1...
                >
                if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(1 00-X));
                This is not a god way to get a weighted distribution (and the OP is
                NOT asking for a weighted distribution) - you should just generate one
                floating point number and apply a transformation function to it.
                Continuous transformations are quite difficult but since for a
                possible range of 100 integer values a discontinuous transformation
                would be adequate - something like:

                function transform($in)
                {
                if (($in<0) || ($in>100)) trigger_error(' out of range');
                $mul=array(
                1=>10, 5=>9, 10=>7, 15=>6....40=>1, 50=>0.9, 55=>0.8...
                );
                return($in * $mul[5*(integer)($in/20)]);
                }

                Note the values above will emphasize the mean rather than what the OP
                was asking for, but could be adapted for purpose by tweaking the $mul
                values.

                C.

                Comment

                • The Natural Philosopher

                  #9
                  Re: Need help to custom the rand function ,

                  C. (http://symcbean.blogspot.com/) wrote:
                  On 31 Jan, 19:29, The Natural Philosopher <a...@b.cwrot e:
                  >Fred wrote:
                  >>Hi guys,
                  >>I was wondering how could i get a random number exemple between 1 and
                  >>100 but with n% of getting some value over a number x.
                  >>Thanks a lot in advance.
                  >>Fred
                  >Never mind Jerry, No one else does.
                  >>
                  >So you want a random number from a weighted distribution?
                  >>
                  >Your spec doesn't contain quite enough data to formulate the formula,
                  >since there are an infinite number of distributions that have N% over
                  >value X..
                  >>
                  >One example, is that you first of all get a random number between 0 and
                  >100: If this is greater than N, then select a random number greater than
                  >X, else select one less than X.
                  >>
                  >So in pseudo code where RAND(X) returns a number between 0 and X-1...
                  >>
                  >if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(1 00-X));
                  >
                  This is not a god way to get a weighted distribution (and the OP is
                  NOT asking for a weighted distribution)
                  (a) I never said it was.
                  (b) Agreed. But some weighting is inherent in what he said he wanted.
                  (c) it is one way to get what he said he wanted though.

                  The problam is he wnats SOME form of weighted distribution, ..as evinced
                  by the actual spec. But there isn't enough to say WHAT weighting he wants.

                  - you should just generate one
                  floating point number and apply a transformation function to it.
                  Continuous transformations are quite difficult but since for a
                  possible range of 100 integer values a discontinuous transformation
                  would be adequate - something like:
                  >
                  function transform($in)
                  {
                  if (($in<0) || ($in>100)) trigger_error(' out of range');
                  $mul=array(
                  1=>10, 5=>9, 10=>7, 15=>6....40=>1, 50=>0.9, 55=>0.8...
                  );
                  return($in * $mul[5*(integer)($in/20)]);
                  }
                  >
                  Note the values above will emphasize the mean rather than what the OP
                  was asking for, but could be adapted for purpose by tweaking the $mul
                  values.
                  >
                  Indeed.
                  C.

                  Comment

                  • Fred

                    #10
                    Re: Need help to custom the rand function ,

                    On 31 jan, 21:39, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                    Fred wrote:
                    On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrot e:
                    Fred wrote:
                    >Hi guys,
                    >I was wondering how could i get a random number exemple between 1 and
                    >100 but with n% of getting some value over a number x.
                    >Thanks a lot in advance.
                    >Fred
                    Never mind Jerry, No one else does.
                    >
                    So you want a random number from a weighted distribution?
                    >
                    Your spec doesn't contain quite enough data to formulate the formula,
                    since there are an infinite number of distributions that have N% over
                    value X..
                    >
                    One example, is that you first of all get a random number between 0 and
                    100: If this is greater than N, then select a random number greater than
                    X, else select one less than X.
                    >
                    So in pseudo code where RAND(X) returns a number between 0 and X-1...
                    >
                    if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(1 00-X));
                    >
                    Thanks for the info, i wont mind on him then =)
                    >
                    Almost there with the code,
                    >
                    Let me add some real number in the exemple :
                    >
                    i know before the random what number i must have over let take 50 for
                    the exemple,
                    i also need the random to still pick between 0 and 100, but with a
                    chance of 60% (exemple) to draw something over 50 .
                    >
                    Thanks
                    >
                    Actually, the troll "Natural Philosopher" is one one who should be
                    ignored. He seldom has anything to say that is correct. I was just
                    pointing out you really aren't looking for a random number, from your
                    description. Maybe a minor point to you, but very important when
                    creating an algorithm.
                    >
                    The real question here is, what kind of distribution do you want? For
                    example, do you want a linear distribution, an exponential or
                    logarithmic distribution, or like you said here, a 60% chance of being
                    over 50?
                    >
                    In the case you cited, you want 1-50 to occur 40% of the time, and
                    51-100 to occur 60% of the time (approximately) . Something like this
                    might work (not tested but the algorithm should work):
                    >
                    function notReallyRand() (
                    $temp = mt_rand(0,999);
                    if ($temp < 400)
                    return intval($temp/8);
                    else
                    return intval((($temp-400) / 12) + 50);
                    >
                    }
                    >
                    It gets a random number between 0 and 999. If the random number is
                    between 0 and 399 (inclusive), it returns value / 8 as an int (0-49).
                    >
                    If the value is >= 400, it subtracts 400 from the value (0-599) and
                    divides by 12 (0-49.xx). It add 50 to this and returns the int value
                    (50-99).
                    >
                    You can adjust values as necessary.
                    >
                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstuck...@attgl obal.net
                    =============== ===
                    Thats exaclty the kind of algorythm i needed thanks a lot man , you
                    should have said that first =)
                    Ive tweak it a bit and its doiing a great job !

                    Thanks again !

                    Fred

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: Need help to custom the rand function ,

                      Fred wrote:
                      On 31 jan, 21:39, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                      >Fred wrote:
                      >>On 31 jan, 14:29, The Natural Philosopher <a...@b.cwrot e:
                      >>>Fred wrote:
                      >>>>Hi guys,
                      >>>>I was wondering how could i get a random number exemple between 1 and
                      >>>>100 but with n% of getting some value over a number x.
                      >>>>Thanks a lot in advance.
                      >>>>Fred
                      >>>Never mind Jerry, No one else does.
                      >>>So you want a random number from a weighted distribution?
                      >>>Your spec doesn't contain quite enough data to formulate the formula,
                      >>>since there are an infinite number of distributions that have N% over
                      >>>value X..
                      >>>One example, is that you first of all get a random number between 0 and
                      >>>100: If this is greater than N, then select a random number greater than
                      >>>X, else select one less than X.
                      >>>So in pseudo code where RAND(X) returns a number between 0 and X-1...
                      >>>if ((RAND(100))<N) THEN RETURN RAND(X) ELSE RETURN(X+RAND(1 00-X));
                      >>Thanks for the info, i wont mind on him then =)
                      >>Almost there with the code,
                      >>Let me add some real number in the exemple :
                      >>i know before the random what number i must have over let take 50 for
                      >>the exemple,
                      >>i also need the random to still pick between 0 and 100, but with a
                      >>chance of 60% (exemple) to draw something over 50 .
                      >>Thanks
                      >Actually, the troll "Natural Philosopher" is one one who should be
                      >ignored. He seldom has anything to say that is correct. I was just
                      >pointing out you really aren't looking for a random number, from your
                      >description. Maybe a minor point to you, but very important when
                      >creating an algorithm.
                      >>
                      >The real question here is, what kind of distribution do you want? For
                      >example, do you want a linear distribution, an exponential or
                      >logarithmic distribution, or like you said here, a 60% chance of being
                      >over 50?
                      >>
                      >In the case you cited, you want 1-50 to occur 40% of the time, and
                      >51-100 to occur 60% of the time (approximately) . Something like this
                      >might work (not tested but the algorithm should work):
                      >>
                      >function notReallyRand() (
                      > $temp = mt_rand(0,999);
                      > if ($temp < 400)
                      > return intval($temp/8);
                      > else
                      > return intval((($temp-400) / 12) + 50);
                      >>
                      >}
                      >>
                      >It gets a random number between 0 and 999. If the random number is
                      >between 0 and 399 (inclusive), it returns value / 8 as an int (0-49).
                      >>
                      >If the value is >= 400, it subtracts 400 from the value (0-599) and
                      >divides by 12 (0-49.xx). It add 50 to this and returns the int value
                      >(50-99).
                      >>
                      >You can adjust values as necessary.
                      >>
                      >--
                      >============== ====
                      >Remove the "x" from my email address
                      >Jerry Stuckle
                      >JDS Computer Training Corp.
                      >jstuck...@attg lobal.net
                      >============== ====
                      >
                      Thats exaclty the kind of algorythm i needed thanks a lot man , you
                      should have said that first =)
                      Ive tweak it a bit and its doiing a great job !
                      >
                      Thanks again !
                      >
                      Fred
                      >
                      I would have - had you been clear about what you wanted!

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      Working...