random without replacement

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

    #1

    random without replacement

    Hi Folk

    I want a random list of numbers between 0 and 58 where each number can only
    occur once. I have written this ($f is an array):

    for($i = 0; $i < 13; $i++) {
    $rand[$i] = mt_rand(0,count ($f));
    }

    what else do I need?

    TIA
    [color=blue]
    > Nicolaas[/color]



  • windandwaves

    #2
    Re: random without replacement

    windandwaves wrote:[color=blue]
    > Hi Folk
    >
    > I want a random list of numbers between 0 and 58 where each number
    > can only occur once. I have written this ($f is an array):
    >
    > for($i = 0; $i < 13; $i++) {
    > $rand[$i] = mt_rand(0,count ($f));
    > }
    >
    > what else do I need?
    >
    > TIA
    >[color=green]
    >> Nicolaas[/color][/color]

    This is what I came up with:

    for($i = 0; $i < 12; $i++) {
    $r[$i] = mt_rand(0,count ($f));
    for($j = ($i - 1); $j > -1; $j--) {
    if($r[$j] == $r[$i]) {
    $i = 0;
    }
    }
    }
    but it does not seem to work

    Any suggestions?

    TIA
    [color=blue]
    > Nicolaas[/color]


    Comment

    • yxod

      #3
      Re: random without replacement

      a simple word of advice: Do not use variable names that are the same
      with php functions. If at any point you forget the $, strange things
      will happen. check out rand().

      and now that I am on it:

      // function that generates a random number that does not exists in an
      array
      function get_random_numb er($random_arra y) {
      $random_number= mt_rand(0,58);

      if(is_array($ra ndom_array)&&in _array($random_ number,$random_ array)) {
      return get_random_numb er($random_arra y); // recursiveness
      } else {
      return $random_number;
      }
      }

      // fill the array
      for($i=0; $i<13; $i++) {
      $random_array[$i]=get_random_num ber($random_arr ay);
      }

      // just to showcase
      foreach($random _array as $number) {
      echo $number.'<br>';
      }

      Comment

      • Kimmo Laine

        #4
        Re: random without replacement

        "windandwav es" <winandwaves@co ldmail.com> wrote in message
        news:qFRRf.6343 $JZ1.201628@new s.xtra.co.nz...[color=blue]
        > Hi Folk
        >
        > I want a random list of numbers between 0 and 58 where each number can
        > only occur once. I have written this ($f is an array):
        >
        > for($i = 0; $i < 13; $i++) {
        > $rand[$i] = mt_rand(0,count ($f));
        > }
        >
        > what else do I need?[/color]

        $randomstuff = range(0,58);
        shuffle($random stuff);

        print_r($random stuff);

        --
        "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
        spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


        Comment

        • Tyxod

          #5
          Re: random without replacement

          The "shuffle" thing does the work but produces an array with 58
          elements. And it works a lot faster than the recursive thing (it
          matters if the function is used more than a few times in a script).

          But if you need just 13 (or anything else <>58) items in the array the
          recursive function is really the only sane way to go.

          (of course one could "shuffle($rando mstuff);" and then pick up the
          first 13 items from the resulted array, but I really dont like doing
          that)

          Comment

          • Mladen Gogala

            #6
            Re: random without replacement

            On Wed, 15 Mar 2006 23:05:09 +1300, windandwaves wrote:
            [color=blue]
            > I want a random list of numbers between 0 and 58 where each number can only
            > occur once. I have written this ($f is an array):[/color]

            You should use shuffle() function. Are you trying to win powerball?

            --
            大红鹰娱乐平台采用顶级加密技术,确保用户数据与交易的安全无虞,让玩家能够放心畅玩。


            Comment

            • Jerry Stuckle

              #7
              Re: random without replacement

              Tyxod wrote:[color=blue]
              > The "shuffle" thing does the work but produces an array with 58
              > elements. And it works a lot faster than the recursive thing (it
              > matters if the function is used more than a few times in a script).
              >
              > But if you need just 13 (or anything else <>58) items in the array the
              > recursive function is really the only sane way to go.
              >
              > (of course one could "shuffle($rando mstuff);" and then pick up the
              > first 13 items from the resulted array, but I really dont like doing
              > that)
              >[/color]

              Why not?

              1. It's much clearer than the recursive code
              2. It's faster than the recursive code

              If course - it does use 156 more bytes than the recursive solution. But
              the parser will probably need more than this just to handle the extra
              code anyway.

              Sometimes saving a few bytes just isn't worth it!

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

              Comment

              • Tyxod

                #8
                Re: random without replacement

                When I say that "I really dont like doing that", I mean that I really
                don't LIKE that! :) It has nothing to do with the semantics of it, and
                I agree with any other solution that worx, but I do not enjoy coding
                like that. That's just me...

                Comment

                • Kimmo Laine

                  #9
                  Re: random without replacement

                  "Tyxod" <yrizos@gmail.c om> wrote in message
                  news:1142426997 .598055.231730@ j52g2000cwj.goo glegroups.com.. .[color=blue]
                  > When I say that "I really dont like doing that", I mean that I really
                  > don't LIKE that! :) It has nothing to do with the semantics of it, and
                  > I agree with any other solution that worx, but I do not enjoy coding
                  > like that. That's just me...[/color]

                  Well, I'm different. I don't like to "reinvent the wheel" every time I code
                  something, if such functionality already exists in the function library, I
                  use it rather than rewrite the whole thing.

                  As for the list being 58 elements instead of just 13, it can be easily
                  truncated to that using array_slice($ra ndomstuff,-13) after the shuffle() so
                  that ain't no problem either.

                  It's just that it took me a minute or two to come up with the range and
                  shuffle solution, while writing the recursive function would mean a lot more
                  work. I like to keep it neat and tidy, short and simple. Good old KISS and
                  LOVE rules apply, Keep It Simple, Stupid and Leave Out Virtually Everything.

                  --
                  "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
                  spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


                  Comment

                  • Tyxod

                    #10
                    Re: random without replacement

                    yeap, true, but there's no fun in that...

                    Comment

                    • d

                      #11
                      Re: random without replacement

                      "Tyxod" <yrizos@gmail.c om> wrote in message
                      news:1142429214 .729790.27680@u 72g2000cwu.goog legroups.com...[color=blue]
                      > yeap, true, but there's no fun in that...[/color]

                      php is about efficiency first, fun second :) It's hard to have fun with a
                      project that has more wires than R2D2 having sex with C3P0 in a ransacked
                      circuit city - when it breaks, the fun quickly turns to abject horror ;)

                      Best practices are called best practices for a reason...

                      dave


                      Comment

                      • sid.bachtiar@gmail.com

                        #12
                        Re: random without replacement

                        > yeap, true, but there's no fun in that...

                        What's so fun about re-inventing the wheel? :-\

                        Comment

                        • windandwaves

                          #13
                          Re: random without replacement

                          Mladen Gogala wrote:[color=blue]
                          > On Wed, 15 Mar 2006 23:05:09 +1300, windandwaves wrote:
                          >[color=green]
                          >> I want a random list of numbers between 0 and 58 where each number
                          >> can only occur once. I have written this ($f is an array):[/color]
                          >
                          > You should use shuffle() function. Are you trying to win powerball?[/color]

                          Not sure about the powerball thing. But this is how I did it in the end:

                          function randomizer($arr ay, $itemsneeded) {
                          $r = array_rand($arr ay, $itemsneeded);
                          foreach($r as $c) {
                          $v[$c] = $array[$c];
                          }
                          return $v;
                          }


                          Comment

                          • Geoff Berrow

                            #14
                            Re: random without replacement

                            Message-ID: <qFRRf.6343$JZ1 .201628@news.xt ra.co.nz> from windandwaves
                            contained the following:
                            [color=blue]
                            >
                            >I want a random list of numbers between 0 and 58 where each number can only
                            >occur once. I have written this ($f is an array):
                            >
                            >for($i = 0; $i < 13; $i++) {
                            > $rand[$i] = mt_rand(0,count ($f));
                            >}
                            >
                            >what else do I need?[/color]

                            Here is my lottery ball picker

                            <?php
                            asort($rand_key s = array_rand(arra y_fill(1, 49,''), 6));
                            print "Your Lotto Numbers are: <big>".implode( $rand_keys,", ")."</big>";
                            ?>

                            --
                            Geoff Berrow (put thecat out to email)
                            It's only Usenet, no one dies.
                            My opinions, not the committee's, mine.
                            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                            Comment

                            • Jim Carlock

                              #15
                              Re: random without replacement

                              <sid.bachtiar@g mail.com> wrote[color=blue]
                              > What's so fun about re-inventing the wheel? :-\[/color]

                              Perhaps you end up with a better wheel? Less friction? More MPG?

                              Perhaps you end up with no wheels at all and everything slides or
                              hovers along?

                              There's a lot of fun in reinventing the wheel. And an opportunity to
                              make something better.

                              Wheels work with friction. Lessen the friction and you end up with a
                              better wheel... and perhaps no braking mechanism! That in turn leads
                              to more revenue because the brakes require reinvention.

                              Jim Carlock
                              Reinvent the car.
                              Less than 3 years of oil left inside the United States Oil Reserves.
                              Post replies to the group.


                              Comment

                              Working...